My Blog List

Friday 25 November 2011

How to Handle Status Bar Notification in Android.

Hello friends,
Good Afternoon ;-)
This is another post for  android developer. This time we will notify user with Status Bar Notification .
So first of all some basics about notification in android.
With the help of Notification you can notify user for particular event occurrence, like incoming sms, mail, downloading data or some services are running .So for handle notification in your own app.  Android provide two classes :
1. NotificationManager
2.Notification
Notification class sets the properties like icon,ticker text,sound,vibration and all that.
Whereas
The NotificationManager is an Android system service that executes and manages all status bar notifications. You do not instantiate the NotificationManager directly. In order to give it your Notification, you must retrieve a reference to the NotificationManager with getSystemService() and then, when you want to notify the user, pass it your Notification with notify().
So here is the sample project for Notification.
1.Create new project  file->new->Android Project.
2.Create  your main.xml file.
3.Do some code  in your main.java file.


package com.bp.notifyme;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class NotifyMe extends Activity {
    private static final int HELLO_ID = 1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void notifyMe(View view)
    {
        NotificationManager notificationmanager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification(R.drawable.msg, "You have notification",System.currentTimeMillis());
        Intent notificationIntent=new Intent(this,Next.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(getApplicationContext(), "MyNotification", "This is notification Demo", contentIntent);


        notificationmanager.notify(HELLO_ID, notification);
    }
}
4.Create one another Next.java class,do nothing in this class.
5.Run your project in emulator. here is the o/p from my emulator.














Thank you
good day ;-)
for any query feel free to mail me : bpsingh216@gmail.com

1 comment: