英文:
Fucntion inside service stops after app is closed
问题
以下是翻译好的内容:
这个服务始终在后台运行,但由于某种原因,我的函数 saveUserLocation();
在一段时间后停止工作。当应用程序处于打开状态时,它会一直运行。这里有一些图片显示在我关闭应用程序后,服务仍然在后台运行,另一张图片中我关闭应用程序,然后 logD
仍然会打印一些秒钟,然后停止。
后台中的服务
LogD -> logcat 突然停止
服务代码
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: 被调用");
notificationGenerator();
getLocation();
return START_NOT_STICKY;
}
private void getLocation() {
// ---------------------------------- LocationRequest ------------------------------------
// 创建位置请求以开始接收更新
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, "(*) 服务正在运行");
}
},
Looper.myLooper()); // Looper.myLooper 告诉此方法一直重复,直到线程被销毁
}
清单
<service android:name=".service.Service"
android:enabled="true"
android:exported="true"/>
在 Logcat 中没有显示任何错误,它只是停止了,当我再次启动应用程序时,它继续运行。有什么原因导致它停止的想法吗?
英文:
The service runs in the background all the time, but for some reason my function saveUserLocation();
, stop after some time. When the app is open it runs the whole time. Here is some pictures to show the service still runs in the background after I close the app, and the other picture I close the app and then the logD
still prints for some seconds, then it stops.
service in background
LogD -> logcat suddenly just stops
Code from service
@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
}
Manifest
<service android:name=".service.Service"
android:enabled="true"
android:exported="true"/>
It dosen't say any error in Logcat, it just stops, and when I start the app again it continues. Any ideas why it stops?
答案1
得分: 0
应用程序关闭时,服务也会关闭。因为它们在同一个线程中,所以该服务应该在另一个线程上运行,以便它不会被关闭,所以有两种方法可以保持服务运行。
1:使用闹钟管理器(请参考此链接:https://stackoverflow.com/a/34790376/8918150)
2:您还可以使用startForeground()。这是一个很好的方法来实现这一点。
英文:
When app is closed the service gets also closed. Because they are in one thread, so that service should be run on another thread so it cannot be closed, So there is two way to alive the service.
1:Use alarm amanger (Follow this https://stackoverflow.com/a/34790376/8918150)
2:You can also use startForeground(). And this is a good approach to do that
专注分享java语言的经验与见解,让所有开发者获益!
评论