在某些设备上,即使使用START_STICKY,应用程序被关闭后服务仍无法重新启动。

5
在我的服务中,我已经返回了START_STICKY,以便在我杀死应用程序后重新启动我的Service
我已经测试过,在三星、索尼和LG设备上可以正常工作,但在小米设备上无法工作(服务未能重新启动)。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

我该如何处理这种情况?非常感谢您的帮助和建议。
来自安卓文档。
/**
 * Constant to return from {@link #onStartCommand}: if this service's
 * process is killed while it is started (after returning from
 * {@link #onStartCommand}), then leave it in the started state but
 * don't retain this delivered intent.  Later the system will try to
 * re-create the service.  Because it is in the started state, it will
 * guarantee to call {@link #onStartCommand} after creating the new
 * service instance; if there are not any pending start commands to be
 * delivered to the service, it will be called with a null intent
 * object, so you must take care to check for this.
 * 
 * <p>This mode makes sense for things that will be explicitly started
 * and stopped to run for arbitrary periods of time, such as a service
 * performing background music playback.
 */
public static final int START_STICKY = 1;

重写服务中的 OntaskRemoved 方法,然后重新启动服务。 - Divyesh Patel
不幸的是,在小米设备上,ontaskRemoved也无法被调用,就像我在这里提出的问题一样:http://stackoverflow.com/questions/41629152/service-ontaskremoved-not-call-when-kill-app-in-some-device - Linh
如果是这样,我猜测这可能是小米厂商特定的问题。 - Onik
1
可能是重复的问题:onTaskRemoved()在华为和小米设备上没有被调用 - Yoni Gross
定义“kill the app”。 - Kevin Krumwiede
1个回答

2

一些设备,如小米和vivo,有一个额外的权限称为“自启动”,允许您的应用程序服务在被杀死后自动重新启动。我想你可以在小米的附加权限中找到它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接