android - 如何在强制停止应用程序后重新启动服务

标签 android service

这是我的服务

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/

相关文章:

android - 适用于 Android 的 Google Play 音乐 API

android - 使用httppost android上传文件

android - collapsingToolbarLayout 仅在折叠时设置标题

service - 为什么我应该更喜欢 OSGi 服务而不是导出的包?

vue2JS 中的服务 - 创建钩子(Hook)时出错

java - 忽略调试库中的 java 代码

android - 了解何时在 Android 中使用 cordova.file.externalRootDirectory

delphi - 没有主图标的服务应用程序

windows - 如何获取 Windows 7 上所有服务的列表?

java - 我可以从 osgi 控制台获取有关捆绑服务的哪些信息?