Android: How To Schedule An Alarmmanager Broadcast Event That Will Be Called Even If My Application Is Closed?
Solution 1:
Use AlarmManager.setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
for this. Set the type to AlarmManager.RTC_WAKEUP
to make sure that the device is woken up if it is sleeping (if that is your requirement).
Something like this:
Intentintent=newIntent("com.foo.android.MY_TIMER");
PendingIntentpendingIntent= PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManagermanager= (AlarmManager) getSystemService(ALARM_SERVICE);
longnow= System.currentTimeMillis();
longinterval=60 * 60 * 1000; // 1 hour
manager.setRepeating(AlarmManager.RTC_WAKEUP, now + interval, interval,
pendingIntent); // Schedule timer for one hour from now and every hour after that
You pass a PendingIntent to this method. You don't need to worry about leaking Intents.
Remember to turn the alarm off by calling AlarmManager.cancel()
when you don't need it anymore.
- No Sound When The Activity Starts From Lock Screen
- Setrepeating() Of Alarmmanager Repeats After 1 Minute No Matter What The Time Is Set (5 Seconds In This Case, Api 18+)
- How To Draw Brushes Or Shape Using Path When Moving Finger Fast In Andorid Canvas (generate Missing Point When User Move Finger Fast)
Don't register a Receiver in code for this. Just add an <intent-filter>
tag to the manifest entry for your BroadcastReceiver, like this:
<receiverandroid:name=".MyReceiver"><intent-filter><actionandroid:name="com.foo.android.MY_TIMER"/></intent-filter></receiver>
Solution 2:
You need to user an Android Component Called Service
for this. From the service code you can schedule your Task using the AlarmManager with PendingIntent Class for every hours. As your AlarmManger is declared in the Service Components it doesn't require any GUI and will get execute in background, till you have battery in your device.
Post a Comment for "Android: How To Schedule An Alarmmanager Broadcast Event That Will Be Called Even If My Application Is Closed?"