服务在后台不工作
Service doesn't work in background
我的服务是这样的:
Handler mHandler = new Handler();
private void ping() {
//My codes
scheduleNext();
}
private void scheduleNext() {
mHandler.postDelayed(new Runnable() {
public void run() {
ping();
}
}, 10000);
}
public int onStartCommand(Intent intent, int x, int y) {
mHandler = new android.os.Handler();
ping();
return START_STICKY;
}
当应用程序未被终止但不是 phone 的当前程序时,这有效,但当我终止应用程序时,它不起作用。
当应用程序被杀死时,我怎样才能做到 运行?谢谢
如果您希望您的服务在应用程序被终止时仍能正常工作,您必须将其设置为 ForegroundService
,并使用 startForeground (int id, Notification notification)
启动它。您可以阅读更多相关信息 here 。
我的服务是这样的:
Handler mHandler = new Handler();
private void ping() {
//My codes
scheduleNext();
}
private void scheduleNext() {
mHandler.postDelayed(new Runnable() {
public void run() {
ping();
}
}, 10000);
}
public int onStartCommand(Intent intent, int x, int y) {
mHandler = new android.os.Handler();
ping();
return START_STICKY;
}
当应用程序未被终止但不是 phone 的当前程序时,这有效,但当我终止应用程序时,它不起作用。 当应用程序被杀死时,我怎样才能做到 运行?谢谢
如果您希望您的服务在应用程序被终止时仍能正常工作,您必须将其设置为 ForegroundService
,并使用 startForeground (int id, Notification notification)
启动它。您可以阅读更多相关信息 here 。