Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

alarmmanager - How to save scheduled alarm after app was killed by Android or task killer?

Code that schedules alarm.

    PendingIntent sender = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, time, sender);

Its working fine, but when I kill my app in task killer, I lost my scheduleed alarm. How to solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

have your application broadcast a message as its being killed, and when this message is broadcast, then have a listener check if the service is still running.. if its not run it. This will insure that your service is running even if the application is killed.

Update

I'll try to create a flow diagram for you

Death/Restart of a service

The onDestroy() method is part of a service.

I hope this helps.

UPDATE 2

One thing I forgot to mention is the fact that you ideally only want one instance of the service to be run. So just looking at the ID that is present within the onStart() should be == to 1 to start it else.. ignore it.

Methods of notice of the Service Class:

onStart() : This method is called when the service is being started

onDestroy() : This is the method that is called when a service is being killed

Methods of notice of the BroadcastReciever class:

onReceive(): This methods receives all intents that are sent to it (unless filtered)

Look up examples on BroadcastRecievers (Message Broadcasting) and Service (Starting a service)

References:

http://developer.android.com/reference/android/content/BroadcastReceiver.html

http://developer.android.com/reference/android/app/Service.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...