0

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...

drulabs
  • 3,071
  • 28
  • 35

3 Answers3

1

You need something like this:

 public RepeatAlarm(Context context, Bundle extras, int timeoutInSeconds){
     AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent intent = new Intent(context, RepeatAlarm.class);
     intent.putExtra(REMINDER_BUNDLE, extras);
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     Calendar time = Calendar.getInstance();
     time.setTimeInMillis(System.currentTimeMillis());
     time.add(Calendar.SECOND, timeoutInSeconds);
     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),timeoutInSeconds*1000, pendingIntent);
 }

I pass the application context to it.

Btw in your main class you could declare a static context and then use it in any other class of your project whenver you want the applications context.

Miky
  • 942
  • 2
  • 14
  • 29
1

I use this code to activate periodic alarms via broadcast receiver:

public class UpdateReceiver extends BroadcastReceiver {

    public static final String LOG_TAG = "camerawatch.alarmactivator";
    public static final String ACTION = "de.pribluda.android.camerawatch.UPDATE_ALARM";

    public static void activate(Context context) {
        AlarmManager alarmService = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
        Log.d(LOG_TAG, "pending intent: " + pendingIntent);
        // if no intent there, schedule it ASAP
        if (pendingIntent == null) {
            pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            // schedule new alarm in 15 minutes
            alarmService.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),300000, pendingIntent);
            Log.d(LOG_TAG, "scheduled intent: " + pendingIntent);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(LOG_TAG, "received intent: " + intent);
        CameraWidgetProvider.displayCurrentState(context);
    }
}

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

J. Rahmati
  • 735
  • 10
  • 37
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • it works... thanks.... On addition is I added name to the intent and I am filtering the intent with the name... so no need to specify the alarm receiver in intent. Thanks :D – drulabs Dec 15 '11 at 05:19