开发者

pendingintent on alarms for android

开发者 https://www.devze.com 2023-02-10 18:34 出处:网络
When setting a service to go off at particular time, I use the AlarmManager system service. Everything goes off without a problem, service is called and actions take place.

When setting a service to go off at particular time, I use the AlarmManager system service. Everything goes off without a problem, service is called and actions take place.

When the alarm time is rea开发者_如何学Goched, the service starts, and at this point I get the system time (System.currentTimeMillis()). I'm guessing this wont be the actual time the service start. Is there a way to get the time that was set for this PendingIntent?

ie

Set alarm for 9am.

DoStuffService starts at 9am.

DoStuffService knows it was supposed to start at 9am, and uses this value for future functions.


When you create an intent for your alarm, you could put extra data, including time of the alarm, into it like this:

Intent intent = new Intent("action name");

//put extra data into the intent:
intent.putExtra("alarm_time_hours", hours);
intent.putExtra("alarm_time_minutes", minutes);

PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

Then in your receiver or service you need to get this extra data from received intent. Use something like this:

Bundle bundle = intent.getExtras();
if(bundle.containsKey("alarm_time_hours")) {
    int hours = bundle.getInt("alarm_time_hours");
}
if(bundle.containsKey("alarm_time_minutes")) {
    int minutes = bundle.getInt("alarm_time_minutes");
}


Is there a way to get the time that was set for this PendingIntent?

No, sorry.

However, it should not be terribly difficult for you to determine it yourself. Following your example, if your service reports that it is now 09:00:02.36, you should be able to round down to determine that this is the 9am alarm.

0

精彩评论

暂无评论...
验证码 换一张
取 消