Posts Tagged Status Bar Notification with an example
Status Bar Notification with an Example
Posted by Deepthi Girija Thampi in Android on December 23, 2011
Notification as you know notifies the user of an event or events.A Status Bar Notification is simply the notification which appears on the Status bar of the system.
Now, lets look at how to launch notifications on the status bar.
For this we need two classes:
1)Notification and
2)Notification Manager
Firstly we need to retrieve a reference to NotificationManager(which is a system service in android that executes and manages all the notifications) using getSystemService() method.
This is how it is done:
NotificationManager nman; nman=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Next,we need to create an instance of the Notification class inorder to define the properties of the status bar notification – the icon that appears on the status bar when notification is launched, the notification text ,time of notification etc.
This is how I’ve done in my code:
Long when=System.currentTimeMillis(); Notification n; n=new Notification(R.drawable.greenhome,"1",when);
Next, we need to write the code for what have to be done when the user clicks the specific notification.
Here is how its done:
Context context=getApplicationContext(); String Title1="Alert"; String Text1="Just an alert"; Intent notifintent=new Intent(context,Alerttest.class); PendingIntent pending; pending=PendingIntent.getActivity(context,0,notifintent,android.content.Intent.FLAG_ACTIVITY_NEW_TASK); n.setLatestEventInfo(context, Title1, Text1, pending);
Now, to notify the user pass the notification object here its ‘n’ to NotificationManager:
nman.notify(0,n);
where 0(zero) is just the notification id to uniquely identify a notification.
To stop the notification:
nman.cancel(0);
which specifically cancels the notification with id=0
Or
nman.cancelAll();
which cancels all notifications.
Now that you have got a brief idea about launching a status bar notification, you can easily understand my example code below.
Here, I’m presenting an example in which I’ve used two activities namely MultiNotificationdemoActivity and Alerttest .
In the main activity MultiNotificationdemoActivity I’ve used two buttons ‘start’ and ‘stop’ where ‘start’-starts the notification(here I used two notifications one will lead you to the website of google when clicked and the other when attented will lead you to another activity(Alerttest)) and the ‘stop’ – stops the notifications.
In the second activity Alerttest , there is a TextView with text “WELCOME” and a button which when clicked returns to the first activity.
In my main activity which is MultiNotificationdemoActivity the code for launching notification is given. Just have a look at the code now.
public class MultiNotificationdemoActivity extends Activity { /** Called when the activity is first created. */ Notification n; NotificationManager nman; Button start,stop; Long when=System.currentTimeMillis(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nman=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); start=(Button)findViewById(R.id.start); stop=(Button)findViewById(R.id.stop); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Context context=getApplicationContext(); String Title1="Alert"; String Text1="Just an alert"; String Title2="Browse"; String Text2="Google"; Intent notifintent=new Intent(context,Alerttest.class); Intent ni=newIntent(); ni.setAction(android.content.Intent.ACTION_VIEW).setData(Uri.parse("http:/www.google.com")); PendingIntent pending; pending=PendingIntent.getActivity(context,0,notifintent,android.content.Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent p; p=PendingIntent.getActivity(context,0,ni,android.content.Intent.FLAG_ACTIVITY_NEW_TASK); n=new Notification(R.drawable.greenhome,"1",when); n.flags=Notification.FLAG_AUTO_CANCEL; n.setLatestEventInfo(context, Title1, Text1, pending); nman.notify(0,n); n=new Notification(R.drawable.icon_google,"2",when); n.flags=Notification.FLAG_AUTO_CANCEL; n.setLatestEventInfo(context, Title2, Text2, p); nman.notify(1, n); } }); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub nman.cancelAll(); } }); } }
Note: Here, I’ve used a flag FLAG_AUTO_CANCEL inoder to clear the notification after it is being attended.
Here is how it looks when you first run the Application:
When you click the Start button(just look at the two notifications):
After Dragging down the notification curtain:
After Clicking the Home icon (see how the home icon disappeared):
After Clicking the Google icon(see now the google icon too vanished from status bar):
Android, Android Desk, androiddesk, Multiple Notifications, Notification, Notification in android, Notification Manager, Status Bar Notification, Status Bar Notification with an example
About Me
I'm a beginner in Android and interested in exploring more into the Android World.I love sharing my knowledge because it increases as we do.Come, lets share and thus expand our knowledge space in Android.
Blog Stats
- 381,266 Pageviews
Recent Posts
- Toggle Button inside ListView in android
- Creating Dynamic Views in android
- Blocking a call without user intervention in android with an example
- A Simple ListView in Android
- Bluetooth in Android-Part II
- Bluetooth in Android-Part I
- Started Service in Android
- Service in Android
- Spinner in Android
- AutoCompleteTextView in Android
- Toggle Button in android
- XML layout in android
- How to implement a Button Click event in Android without onClickListener?
- Button in android
- How to change the usual icon of your application in Android?
- Activity in android
- Creating a Progress Dialog in Android:
- Custom Dialog in Android
- Custom Toast Notification in Android with an Example
- Toast Notification in Android with a simple example
- Check the usage of CheckBox in an Android activity
- Dialog in Android
- Just an Introduction to Notifications
- Implicit Intent
- Explicit Intent
- Status Bar Notification with an Example
- Horizontal and Vertical Scrolling in Android
- Fundamental Building Blocks of an Android Application
- Introducing ANDROID:
Archives
- August 2012 (3)
- June 2012 (1)
- May 2012 (2)
- April 2012 (2)
- March 2012 (3)
- February 2012 (5)
- January 2012 (8)
- December 2011 (5)
Android
action Activity in android Alert Dialog in android Alert dialog with list of items Alert Dialog with one button Alert Dialog with two buttons Android android architechture Android Bluetooth Android Desk androiddesk Android Framework Android Runtime Android Tutorial Application components Applications architechture Bluetooth Bluetooth in Android Bound Service in Android Broadcast Receivers Broadcast Receivers in android Building Blocks Button in android Communication between essential components in android components of Android OS Content Providers Contents providers in android Dalvik Virtual Machine data and category dialog Dialog in android DVM essential building blocks Explicit Intent Explicit Intent with an Example Fundamental Building Blocks Horizontal and Vertical Scrolling in android HorizontalScrollView How to move from one activity to another using an intent How to perform Horizontal and Vertical scrolling Implicit Intent implicit intent using an example Intent Intent Filters intents Introducing android Introduction to Notifications Libraries Linux Kernel ListView in Android Mai makeText() making a call using implicit intent Multiple Notifications Notification Notification in android Notification Manager notifications Positioning the Toast Scrolling in android ScrollView sending sms using implicit intent Service in android Services in android setGravity() Started Service in Android Status Bar Notification Status Bar Notification with an example toast Toast Notification Toast Notification with an example Toggle Button in android Use of ScrollViews viewing contacts using implicit intent- Join 114 other subscribers