简体   繁体   English

将Intent Service与Alarm Manager结合使用

[英]Using Intent Service in combination with Alarm Manager

I have used the Alarm Manager to allow the user to schedule a certain task to repeat at a certain amount of time. 我已经使用了警报管理器来允许用户安排特定任务在特定时间重复执行。 In the context of the application, I have an Android game where people are able to schedule when to send their ships. 在该应用程序的上下文中,我有一个Android游戏,人们可以在该游戏中安排发送船的时间。 The Alarm Manager is working fine, alarms get kicked off at the right time etc. 警报管理器工作正常,警报在适当的时间启动等。

What happens when an alarm is fired off (usually every hour and a bit), is that my IntentService will start communicating with the server to send the ships. 发出警报时(通常每隔一个小时便会发出一次),我的IntentService将开始与服务器通信以发送船只。 This action may take 1 minute, but it can last up to 10 minutes. 此操作可能需要1分钟,但最多可能需要10分钟。 Even this all works fine. 即使这一切都很好。 Alarms get fired, ships get sent, everyone happy. 警报被触发,船舶被发送,每个人都很高兴。 The problem arises at night, I go to sleep and expect when waking up that my ships have been sent all night long. 问题出现在晚上,我入睡并且希望醒来时我的船已经整夜被送出。 Nope. 不。 Logging & notifications show that the Alarms are fired correctly, but it appears that the IntentService is killed when it's communicating with the server. 日志和通知显示警报已正确触发,但IntentService与服务器通信时似乎被杀死。

Possible cause for this is that I'm not looking at the game every once in a while like I do when I'm awake, and thus keep some form of process running which prevents the IntentService from being garbage collected. 造成这种情况的可能原因是,我并不是像清醒时那样不时看游戏,因此会保持某种形式的进程运行,从而防止IntentService被垃圾回收。

I've already tried a LOT of things to fix this. 我已经尝试了很多方法来解决此问题。 I used to work with a BroadCastReceiver, I used to spawn ASyncTasks in the IntentService , but I've since refactored my code to not use those things anymore as they're bad practice for Services. 我曾经使用BroadCastReceiver曾经IntentService中生成ASyncTasks ,但是此后我重构了代码以不再使用这些东西,因为它们对服务来说是不好的做法。

I have also checked this resource: http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html but I'm still not sure if what I'm doing is the correct thing to handle this situation. 我还检查了以下资源: http : //techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html,但是我仍然不确定我在做什么是否正确处理这种情况。 I have placed some extensive logging for the next night to review in the morning but I'd like you guys' opinion over this. 我为第二天晚上放置了一些日志记录,供早晨查看,但是我希望你们对此有意见。

Oh I'm also requesting a WakeLock for the complete duration of the onHandleIntent function using: 哦,我还要求使用onHandleIntent函数的完整持续时间来使用WakeLock

PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);                                         
this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "MyApplicationWakeLock");

My IntentService: 我的IntentService:

public class ScheduledRequestService extends IntentService {

    public ScheduledRequestService() {
        super("ScheduledRequestService");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startID){
        Logger.d("OnStartCommand!");
        return super.onStartCommand(intent, flags, startID);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ScheduledRequest request = (ScheduledRequest) intent.getExtras().getSerializable("request");
        // This function will start a lot of client <-> server communication
        request.onExecute(this, intent);
    }

    @Override
    public void onDestroy(){
        Logger.d("OnDestroy!");
        super.onDestroy();
    }
}

So again, my question is; 同样,我的问题是; am I structuring this correctly? 我是否正确构造了这个结构? Should I use a normal Service instead of an IntentService ? 我应该使用普通服务而不是IntentService吗? Can my IntentService get garbage collected while it is handling an Intent (I think I read that this is possible)? 我的IntentService处理Intent时能否将其收集为垃圾(我想我读到这是可能的)?

Can my IntentService get garbage collected while it is handling an Intent (I think I read that this is possible)? 我的IntentService处理Intent时能否将其收集为垃圾(我想我读到这是可能的)?

No, but your process can be terminated. 否,但是您的过程可以终止。

am I structuring this correctly? 我是否正确构造了这个结构?

Not if you are trying to use a _WAKEUP alarm. 如果您尝试使用_WAKEUP警报,则_WAKEUP You need to set things up more carefully in that case, and I would recommend either WakefulBroadcastReceiver or my WakefulIntentService to handle that pattern. 在这种情况下,您需要更仔细地进行设置,我建议您使用WakefulBroadcastReceiverWakefulIntentService来处理该模式。

Should I use a normal Service instead of an IntentService ? 我应该使用普通服务而不是IntentService吗?

No, an IntentService will be fine. 不,可以使用IntentService You may need to consider making it a foreground service using startForeground() . 您可能需要考虑使用startForeground()使其成为前台服务。

Turns out the error was somewhere else than the garbage collector of Android. 原来错误是在Android的垃圾收集器之外的其他地方。 I was using cache which eventually led to me 'running out of ships' because on send time, it deducted the sent amount of ships from the pool. 我使用的是高速缓存,最终导致我“船只用完了”,因为在发送时间,它从池中扣除了已发送的船只数量。 When they returned however, they were never added back to the cache. 但是,当它们返回时,它们永远不会被添加回缓存中。 During the day I probably manually refreshed the cache or forced Android to clear it by using my phone otherwise which didn't cause the problem to arise (as much). 白天,我可能手动刷新了缓存,或者强迫Android使用我的手机清除了缓存,否则并没有引起问题的发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM