Status Bar Notification – Android Notification Sample

Posted: June 17, 2011 in Uncategorized

Android Device- Status bar Notification

Notification

Notification

This is a sample code for displaying notification on status bar of Android Device. You can use this code in service file or in broadcast receiver file or in activity file..

——————————————————————————————————————————————

private NotificationManager mManager;
mManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,"Notification!!", System.currentTimeMillis());
Intent notificationIntent = new Intent(context, SMSActivity.class); 
// if u want to redirect to any activity , u can use intent ike this, which will call when click on notification
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.contentview);

contentView.setImageViewResource(R.id.image, R.drawable.icon);     // for displaying icon on notification
contentView.setTextViewText(R.id.text, "Your text here");   // for displaying text on notification

notification.contentView = contentView;
mManager.notify(500, notification);

—————————————————————————————————————————————-

Below is the content view which used in this example

—————————————————————————————————————————————–

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="3dp"
              >
    <ImageView android:id="@+id/image"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_marginRight="10dp"
              />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#000"
              />
</LinearLayout>

-------------------------------------------------------------------------------------------------
end // ask me if u have any query :)
----------------------------------------------------------------------------------------
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s