【问题标题】:Android FusedLocationProvider stops giving location when phone is idleAndroid FusedLocationProvider 在手机空闲时停止提供位置
【发布时间】:2016-06-30 23:56:49
【问题描述】:
我正在使用 FusedLocationProvider 开发位置跟踪应用程序。我有一个后台服务,每 5 分钟跟踪一次手机的位置。
一切正常,但一旦手机闲置 3 到 4 小时后,后台服务就会停止定位。当用户解锁手机时,跟踪再次开始。
有人可以指导我可能导致问题的原因吗?
【问题讨论】:
标签:
android
geolocation
location
fusedlocationproviderapi
android-fusedlocation
【解决方案2】:
也许您的服务正在停止,因为手机需要释放内存,因此它会终止您的服务。确保您的服务设置为前台服务。
前台服务是一种被认为是用户主动了解的服务,因此不会在内存不足时被系统杀死。前台服务必须为状态栏提供通知,该通知位于“正在进行”标题下,这意味着除非服务停止或从前台删除,否则无法解除通知。
http://developer.android.com/guide/components/services.html
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
【解决方案3】:
Android 会在闲置一段时间后让您的服务进入睡眠状态。您可以使用WakeLock 来防止这种情况发生。
public int onStartCommand (Intent intent, int flags, int startId)
{
PowerManager mgr = (PowerManager)getSystemService(Context.POWER_SERVICE);
mWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
mWakeLock.acquire();
...
return START_STICKY;
}
public void onDestroy(){
...
mWakeLock.release();
}