简体   繁体   English

应用程序自行终止后,是否重新启动了START_STICKY服务?

[英]Is a START_STICKY service restarted after application killed itself

So if I understand right, the difference between START_STICKY and START_NOT_STICKY is that the first will be restarted by the system, in case it kills it. 因此,如果我理解正确, START_STICKYSTART_NOT_STICKY之间的区别在于,第一个将由系统重新启动,以防它被杀死。

Does anyone know whether this also happens when I kill my application using Process.killProcess(Process.myPid()) ? 有谁知道在我使用Process.killProcess(Process.myPid())应用程序时是否还会发生这种情况? Or does the system only restart the service if the service was actually being killed externally, not from within the application process? 还是仅当服务实际上是在外部而不是在应用程序进程中被杀死时,系统才重新启动服务?

Not always, you can't depend on START_STICKY for continuing running the service. 并非总是如此,您不能依靠START_STICKY来继续运行该服务。 Better way is to use startForeground for continuing running or if you want to repeat specific task then schedule your service on specific time. 更好的方法是使用startForeground继续运行,或者如果您想重复执行特定任务,然后在特定时间安排服务。 Also, manage it when the phone is Restarted as well. 另外,在手机重新启动时也要对其进行管理。

Notification.Builder builder = new Notification.Builder(getBaseContext())
                    .setContentTitle(""); 
                    .setContentText("Your content text");
startForeground(1, builder.build());

Yes, the START_STICKY service is restarted after the application is killed by the Android System. 是的,在应用程序被Android系统杀死后,START_STICKY服务会重新启动。

Until and unless you call stopService() method from an app component or stopSelf() from within the service, the service will be restarted if the app is killed. 直到并且除非您从应用程序组件中调用stopService()方法或从服务中调用stopSelf(),否则,如果该应用程序被杀死,该服务将重新启动。

But you can always make the service run using startForeground() method, in which you will have to show a notification in the status bar for your service. 但是您始终可以使用startForeground()方法来运行该服务,在该方法中,您将必须在服务的状态栏中显示一个通知。 If you create a service using this method, your service will run at the same priority as an active activity. 如果使用此方法创建服务,则您的服务将以与活动活动相同的优先级运行。 This means that it is highly unlikely that your service will be stopped, and no restart would be required. 这意味着您的服务极不可能被停止,并且不需要重新启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM