Android AlarmManager in Broadcastreceiver

I have a braodcastreceiver that a broadcast receiver should schedule an alarm.

I usually did

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC, time, myPendingIntent); 

The problem is that getSystemService is not available in the broadcast receiver only in Activty. How can I do it here?

Thanks A.

+8
android broadcastreceiver alarmmanager
source share
1 answer

AndyAndroid,

getSystemService() is part of Context . You will need to save the Context , which you will get in your onReceive() method, for example ...

 private Context mContext; @Override public void onReceive(Context c, Intent i) { mContext = c; } 

Then ... where you call getSystemService() , you use ...

 AlarmManager am = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE); 
+31
source share

All Articles