这是我的服务
public class SService extends Service {
public void onCreate() {
super.onCreate();
someTask();
}
public int onStartCommand(Intent intent, int flags, int startId) {
someTask();
return START_STICKY;
}
public IBinder onBind(Intent intent) {
return null;
}
强制停止应用程序(设置->应用程序->强制停止应用程序)后,我的服务无法运行。怎么解决呢?
最佳答案
根据 Android document
Starting from Android 3.1, the system's package manager keeps track of applications that
are in a stopped state and provides a means of controlling their launch from background
processes and other applications.
Note that an application's stopped state is not the same as an Activity's stopped
state. The system manages those two stopped states separately.
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It
does this to prevent broadcasts from background services from inadvertently or unnecessarily
launching components of stoppped applications. A background service or application can
override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents
that should be allowed to activate stopped applications.
强制停止应用程序时,Android 只是终止进程 ID。没有警告,对服务/Activity 进行回调。根据 Android document ,当应用程序被终止时,它有可能调用 onPause()。
当我在我的应用程序中尝试时,甚至没有调用 onPause() 。 我认为唯一的方法是使用该 Intent 标志并按照克里斯的建议从另一个应用程序发送它。
关于android - 如何在强制停止应用程序后重新启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21452123/