我正在使用FusedLocationProvider开发位置跟踪应用程序。我有一个后台服务,跟踪手机在每5分钟的位置。
一切都很顺利,但是一旦手机空闲,3到4个小时后,后台服务就会停止并进行定位。当用户解锁手机时,跟踪再次开始。
有人能告诉我是什么导致了这个问题吗?
发布于 2016-03-16 17:17:29
一种可能是Android M Doze模式。当设备拔出并固定一段时间后,系统会尝试通过限制应用程序访问CPU密集型服务来节省电池。在大约1小时不活动后开始打瞌睡模式,然后将定期任务等安排到维护窗口。当用户解锁设备时,将再次关闭打瞌睡模式。
您可以在开发人员文档中找到有关Doze模式的更多信息:http://developer.android.com/training/monitoring-device-state/doze-standby.html
发布于 2016-03-17 22:31:45
也许你的服务正在被停止,因为手机需要释放内存,所以它会终止你的服务。确保您的服务设置为前台服务。
前台服务被认为是用户主动意识到的服务,因此不是系统在内存不足时要终止的候选服务。前台服务必须为状态栏提供通知,该通知放置在“正在进行”标题下,这意味着除非停止服务或从前台移除该服务,否则不能解除通知。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);
发布于 2016-03-30 01:50:19
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();
}
https://stackoverflow.com/questions/36013059
复制相似问题