Monday, 18 June 2012

Scheduling activities, services and broadcasts #Android

AlarmManager comes into play when we need to schedule different task in some future time/date or we need to execute some task repeatedly. AlarmManager is android service provided for scheduling purposes. We can use AlarmManager to start activities/services and to send broadcast in some future date/time. Activities, Services or broadcasts registered with AlarmManager will be executed no matter your application is running or not. You can also make AlarmManager to wake up the device (if in sleep state) on given schedule.

In order to registered application components (activities, services and broadcasts) with AlarmManager, we need to obtain a Pending Intent  of an Intent and the use this Intent to  register with AlarmManager. The difference between Pending Intent and normal Intent is that "they are launched with the your application permissions rather than the one who is launching them".

Below is the comparison of pendingInent and normal Inents for starting activities, services and broadcasts:

PendingInent.getActivity =~ startActivity (Inent)
PendingInent.getService =~ startService(Intent)
PendingInent.getBroadcast =~ sendBroadcast(Inent)

Lets say we want to launch an activity called MyActivity after 5 seconds of the current time. We will:

  • Create an Intent of the activity we want to launch.
  • Obtain a PendingIntent of this Intent.Registered this pending intent with AlarmManager.
 
Intent i = new Intent(getApplicationContext(), MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),3333,i,
PendingIntent.FLAG_CANCEL_CURRENT);
 


Lets quickly explain the getActivity arguments, first one is context, second one is
optional number (lets say identification number, fourth one is the intent we want to launch and the last one is again optional flag (in this case we are telling the system that if this intent (3333) is already running cancel it and run again.

  • Registered it with alarmmanager,

 
//getting current time and add 5 seconds in it 
Calendar cal = Calendar.getInstance(); 
cal.add(Calendar.SECOND, 5); 
//registering our pending intent with alarmmanager 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), pi);
 

First argument of am is the flag telling the system what to do, in this case we are telling the system that wake up the system if its in sleep state while launching the intent on this particular time.
In order to start service or send broadcast we only need to create pending intent
these and pass to alarmmanager, e.g lets say we have service called MyService


 
Intent ii = new Intent(getApplicationContext(), MyService.class);
PendingIntent pii = PendingIntent.getService(getApplicationContext(), 2222, ii,
PendingIntent.FLAG_CANCEL_CURRENT);
 

and for broadcast

 
Intent i= new Intent(); 
i.setAction("my.action"); //the name which we will specify in our receiver 
i.putExtra("counter",234); PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
1111, i,PendingIntent.FLAG_CANCEL_CURRENT);
 


Thats for today. Have a question ? ask!

3 comments:

  1. good explanation...

    ReplyDelete
  2. What are i.setAction, i.putExtra and their specific values for?

    Tanks!

    ReplyDelete
    Replies
    1. in i.setAction we specify the action which a broadcast receiver will be listening for. i.e when this broadcast is sent, receivers which were registered for "my.action" will receive the broadcast. i.putExtra is any data you want to send with it.

      Delete