IntentService,PendingIntent和AlarmManager:无法使进程无限期运行吗? [英] IntentService, PendingIntent, and AlarmManager: Cannot keep process running indefinitely?

查看:311
本文介绍了IntentService,PendingIntent和AlarmManager:无法使进程无限期运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序,它在IntentService和AlarmManager中在后台运行两个服务.一个是消息"服务,该服务发送JSON请求并解析响应,另一个是从LocationManager轮询位置.此处的要求是,它们将无限期运行,直到用户用按钮手动将其停止为止-即使设备的屏幕几天都没有打开.服务绝不能停止.电池寿命不是问题.

I've got a pretty simple app that runs two services in the background, using IntentService with AlarmManager. One is a "messaging" service that sends a JSON request and parses the response, the other polls for location from a LocationManager. The requirement here is that they run on an interval, indefinitely, until manually stopped by the user, with a button - even if the device's screen hasn't been turned on for days. The services must never stop. Battery life is not a concern.

我最低支持的API是4.1,并且我正在4.1、4.2和4.4设备上进行测试.在我的Nexus 7和GPad(分别为4.4.4和4.4.2)上,这些服务将无限期运行并按预期运行.在运行4.2的Galaxy Tab 3上,设备似乎在8小时后进入睡眠状态.或处于闲置状态,然后退出报告位置和轮询. 4.1设备似乎也做同样的事情.

My minimum-supported API is 4.1 and I'm testing on 4.1, 4.2, and 4.4 devices. On my Nexus 7 and GPad, 4.4.4 and 4.4.2, respectively, the services will run indefinitely and work as expected. On a Galaxy Tab 3 running 4.2, the device seemed to go to sleep after 8 hrs. or so of inactivity, and would then quit reporting location and polling. The 4.1 devices seem to do the same.

由于这种预感,我将其添加到消息传递服务中以手动唤醒CPU.每60秒运行一次.所以我希望它可以防止设备进入睡眠状态.

With that hunch, I added this to the messaging service to manually wake the CPU. This one runs every 60 sec. so I had hoped it would prevent the devices from ever going to sleep.

@Override
protected void onHandleIntent(Intent intent)
{                       
    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, 
            "com.service.MESSAGE_SERVICE_WAKE_LOCK");

    wl.acquire();

    //poll for messages via JSON    

    wl.release();               
}

像这样设置AlarmManager(pi = PendingIntent ...在IntentService类中传递):

Setting the AlarmManager like so (pi = PendingIntent...passing in the IntentService class):

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pollInterval, pi);

这似乎可以解决我自己的测试问题,并且这些设备始终在为我报告4.4和4.2上的位置.但是,4.1测试人员报告说,大约8个小时后,它仍然在睡觉.我还在朋友的Galaxy S5(4.4.2)上进行了测试,几个小时后它退出了报告位置.

This appeared to fix things for my own testing, and the devices are consistently reporting location for me on 4.4 and 4.2. However, the 4.1 testers have reported that it still goes to sleep on them after about 8 hrs. I also tested it on a friend's Galaxy S5 (4.4.2) and it quit reporting location after a few hours.

这是我的第一个应用程序,这一部分特别令人沮丧.难道我做错了什么?知道为什么这些服务可能会停止运行吗?乐于提供更多代码(如果有帮助的话)...我至少应该从这里开始.

This is my first app and this part has been especially frustrating. Am I doing something wrong? Any idea why these services might stop running? Happy to provide more code if that helps...thought I'd start with this, at least.

推荐答案

对于Service使用PendingIntent的警报,不能保证将设备唤醒足够长的时间以启动或恢复Service.对于IntentService,该服务一旦处理了所有排队的Intent对象,将自动终止.您需要将PendingIntent用于BroadcastReceiver,该BroadcastReceiver需要一个WakeLock并启动您的Service.您的Service然后可以在完成工作后释放唤醒锁,从而使设备重新进入睡眠状态.

Alarms which use PendingIntent for a Service are not guaranteed to keep the device awake long enough for the Service to be started or resumed. In the case of an IntentService, the service automatically dies once it has processed all queued Intent objects. You'll need to use a PendingIntent for a BroadcastReceiver which takes a WakeLock and starts your Service. Your Service can then release the wakelock when it is done with its work, allowing the device to go back to sleep.

这篇关于IntentService,PendingIntent和AlarmManager:无法使进程无限期运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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