Android Nougat

Calling an IntentService from BroadcastReceiver triggered by AlarmManager


I needed to check a RSS feed once daily and notify the user.  I setup an alarm manager to trigger my BroadcastReceiver class.
AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);           Intent intent = new Intent(this, Receiver.class);

    intent.putExtra(ONE_TIME, Boolean.FALSE);  

    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);

    am.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),               AlarmManager.INTERVAL_DAY , pi);  

The Receiver class calls an IntentService which fetches the updates. And then notifies the user through NotificationManager:

Don't forget to add your receiver to the maifest file! eg..    <receiver android:process=":remote" android:name="Receiver"></receiver>  
public class Receiver extends BroadcastReceiver {

      NotificationManager nm;



@Override

public void onReceive(Context context, Intent intent) {
   
   

    Intent service = new         Intent(context, UpdateNumbers.class);
                 context.startService(service); 

.
.
.
.
.


    nm = (NotificationManager) context
       .getSystemService(Context.NOTIFICATION_SERVICE);

CharSequence from = "Nithin";
     

CharSequence message = "Crazy About Android...";

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
       new Intent(), 0);
     
     

Notification notif = new Notification(R.drawable.icon,
       "You Won...", System.currentTimeMillis());

         notif.defaults=Notification.DEFAULT_ALL;
         notif.flags |= Notification.FLAG_SHOW_LIGHTS;
         notif.ledARGB = 0xff00ff00;
         notif.ledOnMS = 300;
         notif.ledOffMS = 1000;

     notif.setLatestEventInfo(context, from, message, contentIntent);
     notif.flags |= Notification.FLAG_AUTO_CANCEL;
     nm.notify(1, notif); 

Comments