I want to invoke a method of an object from user defined class for a particular interval of time for say 2 hours. The input parameter is a Context object.
I have used AlarmReceiver Broadcast Receiver which I registered in manifest
<receiver android:name=".AlarmReceiver" android:process=":remote"></receiver>
I am trying to start alarm manager through a Broadcast Receiver when a particular activity occurs. For receiving the alarm broadcasts I have created alarmReceiver. Do I need any kind of intent-filter for receiving alarm intents???
My problem is I want the application context inside alarmreceiver, I tried using the context of onReceive method of AlarmReceiver but it throws
android.app.ReceiverRestrictedContext
error.
Any solution for this error? If needed I will publish my full code…
I use this code to activate periodic alarms via broadcast receiver:
Note, that activation is done in static method, so I can call it from almost everywhere.
I schedule alarm if and only there is no such pending intent. As for context – it is available virtually everywhere – In activity ( it is context itself ) in broadcast receiver (serice method parameter) etc.
PS: broadcast receiver is preferable to service, because it does not run all the time. When service method returns, it is stopped and application is potentially disposed ( unles it does something else, but mostly it stays around in background). This provides better battery life