Android 服务未收到位置更新
Posted
技术标签:
【中文标题】Android 服务未收到位置更新【英文标题】:Android Service not receiving Location Updates 【发布时间】:2011-10-03 17:12:11 【问题描述】:尝试在我的服务中获取 LocationUpdates,但是,它似乎从未触发 LocationListener。
我的服务启动一个线程,然后使用 Handler.postDelayed() 循环 - 我最初使用 sleep(30000),但我认为这可能阻止了 locationUpdates。服务所做的只是启动这个线程 - 并且 onStartCommand 返回 STICKY 以保持它运行:
public class TST extends Thread
final int DefaultNetworkTick = 120000; // 2 minutes
final int DefaultLocationTick = 20000; //600000;
boolean CustomLocationUpdateTick = false; // If the network requests faster updates
int CustomLocationUpdateTickMs = 600000; // Default 10 minutes
public boolean kill = false;
public Context context;
Handler handler = new Handler();
Date nextNetworkReadTick;
@Override
public void run()
Log.i("TST", "Start run() iteration");
// Set the ticks to the appropriate values
// Network gets read every 2 minutes
nextNetworkReadTick = new Date();
nextNetworkReadTick.setTime(nextNetworkReadTick.getTime() + DefaultNetworkTick);
Log.i("TST", "Set next Network tick to " + nextNetworkReadTick.toString());
Looper.prepare();
Log.i("TST", "Performing location update setup...");
SetupLocationListenerDefault();
handler.postDelayed(runFunc, 2000);
Looper.loop();
// We do our networking and stuff in here
public Runnable runFunc = new Runnable()
public void run()
Log.i("TST", "run() iteration");
if( new Date().after(nextNetworkReadTick) )
// Set next tick
nextNetworkReadTick = new Date();
nextNetworkReadTick.setTime(nextNetworkReadTick.getTime() + DefaultNetworkTick);
UpdateNetwork();
handler.postDelayed(runFunc, 1000);
;
private void UpdateNetwork()
// TODO Auto-generated method stub
LocationManager locMgr;
PendingIntent locationUpdateIntentPending;
Intent locationUpdateIntent;
LocationListener locationListener = new LocationListener()
@Override
public void onLocationChanged(Location arg0)
// TODO Auto-generated method stub
Log.i("TST", "OnLocationChanged: " + arg0.toString());
@Override
public void onProviderDisabled(String provider)
// TODO Auto-generated method stub
Log.i("TST", "OnProviderDisabled: " + provider);
@Override
public void onProviderEnabled(String provider)
// TODO Auto-generated method stub
Log.i("TST", "OnProviderEnabled: " + provider);
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
// TODO Auto-generated method stub
Log.i("TST", "OnStatusChanged");
;
private void SetupLocationListenerDefault()
// Reset location manager
if( locMgr != null )
locMgr.removeUpdates(locationListener);
locMgr = null;
locMgr = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locMgr.getBestProvider(criteria, true);
locMgr.requestLocationUpdates(provider, DefaultLocationTick, 0, locationListener);
【问题讨论】:
我知道这听起来很明显,但您确定可以更新位置信息吗?我以前见过使用返回网络而不是 gps 的标准,如果你在里面,那么你实际上可能不会依赖信号。您是否在服务之外尝试过此代码以查看是否存在问题,或者将其放入程序的快速存根中以验证是否是服务存在问题? 【参考方案1】:重新启动我的手机并开始获取更新 - 似乎 android 卡在某个地方!
【讨论】:
这不应该是答案,但它只是为我节省了很多挫败感。谢谢! 我多次遇到同样的问题。 Google 位置服务似乎有时会停止工作。以上是关于Android 服务未收到位置更新的主要内容,如果未能解决你的问题,请参考以下文章
屏幕关闭时,前台服务未在 Android 7.0+ 中接收位置更新