Android Alarm Manager with broadcast receiver registered in code rather than manifest

How about this?

Intent startIntent = new Intent("WhatEverYouWant");
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, startPIntent);

And then in your Manifest.xml file:

<receiver android:name="com.package.YourOnReceiver">
   <intent-filter>
       <action android:name="WhatEverYouWant" />
   </intent-filter>
</receiver>

So as far as I know you still have to declare the receiver in the Manifest. I’m not sure if you can set it to a private instance inside of your activity. You could declare an onReceive inside of your activity and call that (if the BroadcastReceiver has an interface. I don’t know if it does.)

Leave a Comment