服务在睡眠模式下被杀死。为什么? [英] Service is killed in sleep mode.Why?

查看:122
本文介绍了服务在睡眠模式下被杀死。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关该主题的几乎所有Stackoverflow答案,但没有一个起作用。

I've read just about every Stackoverflow answer that exists on this topic, but none of them worked.

问题:只要我的设备处于睡眠模式一个小时或更长时间,该服务就会被终止


  • onStartCommand()返回 START_STICKY 并使用 startForeground()

    public int onStartCommand(Intent intent, int flags, int startId) {

        notification = makeStickyNotification(); //I've simplified the irrelevant code, obviously this would be a real notification I build

        startForeground(1234, notification);
        return START_STICKY;
        }


这很好,并且即使设备内存不足,它甚至会重新启动我的服务,但这不足以解决我的设备进入睡眠状态一段时间后出现的问题。

This works fine, and it even restarts my service whenever the device is low on memory, but it is not enough to fix the problem that occurs when my device goes to sleep for a while.


  • 在我的Activity的 onCreate() onStartCommand()我的服务>呼叫呼叫我的服务的广播接收器

  • Using Alarm Manager in onCreate() of my Activity and in onStartCommand() of my Service to call a Broadcast Receiver that calls my service

        Intent ll24 = new Intent(this, AlarmReceiver.class);

        PendingIntent recurringLl24 = PendingIntent.getBroadcast(this, 0, ll24, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*60, recurringLl24); // Every minute


活跃,但又不能解决我的问题

This helps keep my service active, but again, doesn't solve my problem


  • 使用Schedule Task Executor保持活动状态

  • Using Schedule Task Executor to keep it alive

 if (scheduleTaskExecutor == null) {

        scheduleTaskExecutor = Executors.newScheduledThreadPool(1);

        scheduleTaskExecutor.scheduleAtFixedRate(new mainTask(), 0, 1, TimeUnit.SECONDS);

    }
...    
class mainTask implements Runnable {

public void run() {
    // 1 Second Timer
   }
}

这还可以使服务保持活动状态,但不能保持服务状态

This also just keeps the service active but doesn't keep it alive after a long sleep.

单独的任务清单

android: launchMode = singleTop

这什么也没做

我如何(1)不必使手机进入睡眠状态并每小时检查一次就可以测试此问题,以及(2)即使设备处于睡眠状态也可以保持服务运行?

How can I (1) test this issue without having to put my phone to sleep and check every hour and (2) keep my service running despite the device going to sleep?

推荐答案

谋杀之谜已经解决,我知道是什么使我的服务丧生。这就是我所做的:


  1. 在我意识到 startsticky startforeground 之后,警报管理器 scheduleTaskExecutor 甚至 唤醒锁无法保存我的服务,我意识到凶手不可能是Android系统,因为我已采取一切可能的措施来防止该系统杀死我的服务,并且它仍然会被杀死。

  2. 我意识到我需要寻找另一个嫌疑犯,因为该服务不会因为系统而死。为此,我不得不进行调查。我运行了以下命令:

  1. After I realized that startsticky, startforeground, alarmmanager, scheduleTaskExecutor, and even wakelock were unable to save my service, I realized the murderer couldn't be the Android system, because I had taken every measure possible to prevent the system from killing my service and it still would get killed.
  2. I realized I needed to look for another suspect, since the service wasn't dying because of the system. For that, I had to run an investigation. I ran the following command:

adb shell dumpsys活动进程> tmp.txt

这将为我提供所有正在运行的进程的详细日志,他们的系统优先事项。本质上, tmp.txt 将是这个谋杀之谜的侦探。

This would give me a detailed log of all the processes running and their system priorities. Essentially, tmp.txt would be the detective in this murder mystery.


  1. 我仔细浏览了整个文件。看来我的服务已被系统正确分配优先级:

  1. I looked through the file with lots of detail. It looked like my service was prioritized properly by the system:

Proc#31:adj = prcp / FS trm = 0 2205:servicename.service / uID(fg-service)

上面的行表示a的确切优先级在Android设备上运行的进程。 adj = prcp 表示该服务是可见的前台服务。

The above line indicates the exact priority of a process running on the Android device. adj=prcp means the service is a visible foreground service.

这时,我意识到我的服务在运行几个小时后肯定会遇到一些错误,因此我让它运行并终止。死后,我再次生成了 dumpsys 来检查错误:

At this point, I realized that my service must be encountering some error a couple hours after running, so I let it run and die. After it died, I produced a dumpsys again to examine the error:


  1. 这时, tmp.txt 文件中没有将我的服务列为任务。激动的是,我滚动到 dumpsys 的底部并解开了谜底!

  1. At this point, my service wasn't listed as a task in the tmp.txt file. Excited, I scrolled to the bottom of the dumpsys and solved the mystery!




com.curlybrace.ruchir.appName.MyService $ 2.onForeground(MyService.java:199)
在com.rvalerio.fgchecker.AppChecker $ 2.run(AppChecker.java:118)$ android.os.Handler.handleCallback(Handler.java:751)的b $ b android.os.Handler.dispatchMessage(Handler.java:95)的
android.os.Looper.loop的
Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6123)
在java.lang.reflect.Method.invoke(本机方法)在COM处
android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

com.curlybrace.ruchir.appName.MyService$2.onForeground(MyService.java:199) at com.rvalerio.fgchecker.AppChecker$2.run(AppChecker.java:118) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6123) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

导致我的服务被终止的堆栈跟踪显示在这里!从本质上讲,一个可以检查正在使用的前台应用程序的变量在不活动数小时后将变为空,这将导致异常并终止服务!

The stack trace that caused the killing of my service was displayed right there! Essentially, a variable that would check for the foreground app being used would become null after a few hours of inactivity, which would cause an exception, and kill the service!

关键要点:
如果您的服务被终止,并且您已尽一切努力确保它不会被终止,请执行 dumpsys 并检查设备活动过程的实质。我保证您会以这种方式找到问题。

Key Takeaways: If your service is getting killed, and you've done everything you can to make sure that it shouldn't be killed, perform a dumpsys and examine the nitty gritty of your device's activity process. I guarantee you will find the issue that way.

我仍然想将@Khemraj的赏金授予,因为他的回答对于某人可能是一个很好的解决方案谁没有正确启动服务。但是,我接受此答案,因为实际上是解决问题的解决方案。

这篇关于服务在睡眠模式下被杀死。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆