【发布时间】:2020-05-05 04:18:12
【问题描述】:
该服务一直在后台运行,但由于某种原因我的功能saveUserLocation();,一段时间后停止。当应用程序打开时,它会一直运行。这是一些图片,显示关闭应用程序后服务仍在后台运行,另一张图片我关闭应用程序然后logD 仍然打印几秒钟,然后停止。
后台服务
LogD -> logcat 突然停止
服务代码
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: called");
notificationGenerator();
getLocation();
return START_NOT_STICKY;
}
private void getLocation() {
// ---------------------------------- LocationRequest ------------------------------------
// Create the location request to start receiving updates
LocationRequest mLocationRequestHighAccuracy = new LocationRequest();
mLocationRequestHighAccuracy.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequestHighAccuracy.setInterval(UPDATE_INTERVAL);
mLocationRequestHighAccuracy.setFastestInterval(FASTEST_INTERVAL);
mFusedLocationClient.requestLocationUpdates(mLocationRequestHighAccuracy, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Log.d(TAG, "(*) Service Running");
}
},
Looper.myLooper()); // Looper.myLooper tells this to repeat forever until thread is destroyed
}
清单
<service android:name=".service.Service"
android:enabled="true"
android:exported="true"/>
它在 Logcat 中没有说任何错误,它只是停止,当我再次启动应用程序时它继续。任何想法为什么它会停止?
【问题讨论】: