Start activity using AlarmManager, without Broadca

2019-02-11 01:46发布

问题:

I hope that someone has an answer for me:

I wonder if it is possible (and common) to use the AlarmManager for directly starting an Activity. The documentation does not explain this explicitly. It only describes the usage of Broadcastreceivers.

If it is possible to start my Activity directy, where will I receive the Intent (onNewIntent)?

Many thanks Jean-Pierre

回答1:

I wonder if it is possible (and common) to use the AlarmManager for directly starting an Activity.

Yes.

The documentation does not explain this explicitly. It only describes the usage of Broadcastreceivers.

Using a BroadcastReceiver is the most common scenario. It is required for _WAKEUP alarms, if you want the device to reliably wake up when the alarm goes off.

Starting an activity from AlarmManager should be used for "alarm clock"-type applications, and little else. You have no idea what the user might be doing with the phone when your activity comes to the foreground, and they may get very angry with you if they feel that your intrusion is unjustified.

If it is possible to start my Activity directy, where will I receive the Intent (onNewIntent)?

By default, a new instance of the activity will be created. If you use FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP, it will bring forward an existing instance of the activity, and you will get the Intent in onNewIntent().

This sample project demonstrates using AlarmManager this way. This sample project demonstrates user configurable Activity-or-Notification when the alarm goes off. Both of these are relatively deep in a series of tutorials, and so the apps are a bit complex.