Monday, April 26, 2010

AlarmManager and Notification in Android

In this, I am going to explain AlarmManager and Notification. For Notifying Users in Android, we can use mainly 3 methods as,

Dialog
Toast
Notification

The full picture of this can seen here

AlarmManager, we can use in two ways, as

-To start some action, if a particular event happens,
-To do some particular actions in every time interval.

As specified in Android developer site, we will use AlarmManager, when we need to do some actions, even if our application is not running. For normal timing operations use handlers.

For using AlaramManager, first we need to declare the BroadcastReceiver in Manifest file as,

<receiver android:name=".TimeAlarm" />
We need to get the object of the AlarmManager using getSystemService(), then define an intent with parameters as the current context and the BoradcastReceiver class(TimeAlarm). Then define a pendingIntent, we will get the object of it using PendingIntent.getBroadcast(). Now we can call the set() of AlarmManager for doing some action for only once. For repetitive action we have to call setRepeating() of the AlarmManager.

In TimeAlarm, to show to user that our AlarmManager is working we will show a notification in status bar. For that first get the object of NotificationManager using, getSystemService(). Create an object of pendingIntent using PendingIntent.getActivity(). Then create an object of the notification and pass it to the notification manager object.

I created a sample program and the complete source code is,


CustomAlarm.java

  
public class CustomAlarm extends Activity {

AlarmManager am;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setOneTimeAlarm();
}

public void setOneTimeAlarm() {
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (5 * 1000), pendingIntent);
}

public void setRepeatingAlarm() {
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(5 * 1000), pendingIntent);
}

}


TimeAlarm.java


public class TimeAlarm extends BroadcastReceiver {

NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {
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,
"Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
}


The screenshots are,



2 comments:

  1. Creative post. I really like your info. Thanks for sharing.

    emergency notification

    ReplyDelete
  2. Great!..it works! I searched so many sites but didn't find this solution of adding anywhere. Thanks :)

    ReplyDelete

Androidians