【问题标题】:How keeping service when App is Killed instead restart?App被杀死时如何保持服务而不是重新启动?
【发布时间】:2017-04-05 17:32:57
【问题描述】:

我正在使用计数器 (0 - 10) 之类的服务来测试服务,但是当应用程序被终止时,服务已经重新启动而不是继续。

在logcat中显示:

04-05 17:25:47.497 25309-25309/? I/livro: onCreate
04-05 17:25:47.498 25309-25309/? I/livro: onStartCommand
04-05 17:25:48.531 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...0
04-05 17:25:49.571 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...1
04-05 17:25:50.611 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...2
04-05 17:25:51.651 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...3
04-05 17:25:52.692 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...4
04-05 17:25:53.733 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...5
04-05 17:25:54.771 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...6
04-05 17:25:55.811 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...7
04-05 17:25:56.851 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...8
04-05 17:25:57.891 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...9
04-05 17:25:57.891 25309-25309/com.example.augustoc.helloservice I/livro: onDestroy

当关闭应用程序时,计数器已重新启动,但我希望该服务继续。

在 onStartCommand 中:

@Override                                               // ID DESTE SERVIÇO
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"onStartCommand");
        count = 0;
        running = true;

        //delega para a trhead
        new WorkerThread().start();
        return START_STICKY;
    }

我尝试返回 START_STICKY 和 START_NOT_STICKY,两者都不起作用。

【问题讨论】:

  • onStartCommand 正在从某个 onCreate() 方法执行?
  • “应用程序被杀死”是什么意思。你是如何“杀死应用程序”的?当您关闭应用程序时,Service 是被 Android 杀死并自动重新启动,还是 Service 只有在您再次运行应用程序时才重新启动?你在什么设备上测试?
  • “应用程序被杀死”是当我关闭应用程序时。当我关闭应用程序时,服务会自动重新启动,但我希望服务继续而不是重新启动。
  • “关闭应用程序”是什么意思。 Android 应用程序没有“关闭”。我了解您想要什么(Service 不会停止),但我想知道您如何知道 Service 已停止!请回答我的问题。另外,您在什么设备和什么版本的 Android 上进行测试?
  • @DavidK onStartCommand()Service 启动时从 Android 框架调用。它不是从onCreate() 调用的。

标签: android android-service


【解决方案1】:

在创建服务时使用这个常量。将 return START_STICKY; 替换为 return START_REDELIVER_INTENT 添加您的必要逻辑。 START_REDELIVER_INTENT 如果系统在 onStartCommand() 返回后终止服务,则重新创建服务并使用传递给服务的最后一个意图调用 onStartCommand()。依次传递任何未决意图。这适用于正在积极执行应立即恢复的工作的服务。

另外请考虑使用 JobScheduler,您将构造这些 JobInfo 对象并将它们传递给带有 schedule(JobInfo) 的 JobScheduler。当满足声明的条件时,系统将在您的应用程序的 JobService 上执行此作业。当您使用 JobInfo.Builder(int, android.content.ComponentName) 创建 JobInfo 时,您可以确定哪个 JobService 旨在为您的作业执行逻辑。

当您收到回调时,该框架会智能,并尝试尽可能多地批处理和延迟它们。通常,如果您没有为作业指定截止日期,它可以在任何时候运行,具体取决于 JobScheduler 内部队列的当前状态,但它可能会推迟到下一次设备连接到电源时来源。

【讨论】:

  • 确保将增量值保存在 Intent 中,以便在恢复时读取它
  • 谢谢,我不明白其中的逻辑,但你可以给我看一段代码吗?
  • Caspain,你使用了“Resumed”和“Recreate”这两个词,但我需要继续。
  • 是的,它将启用延续,至于代码,我会尝试设计一个合适的。
猜你喜欢
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2013-12-31
相关资源
最近更新 更多