Android Notification Status Bar Example

Android Notification Status Bar Example

Android provides a couple of most important ways of interacting short messages to the user.


  • The foremost is our very much used Toast message.
  • The other may be more complicated Notification.
  • A Notification is actually shown within the Android’s Status bar, both like a small icon, and also a TickerText message.
  • Once the user clicks a Notification, an extended message will appear. Additionally, it is possible to keep an Intent using the Notification which will get fired once the user taps the actual extended message.
A Notification is much effective adaptable feature obviously with a little extra work.

Notification Configuration

A service, running in the background, requires a method to allow users understand something of interest has happened, for example whenever e-mail has been received. Additionally, the actual service may require a way in order to drive the user for an activity just where they are able to do something about the event — reading a received message.

For this, Android gives you status bar icons, flashing lights, and also some other indicators together generally known as “notifications”.
You are able to raise notifications through the NotificationManager. The NotificationManager is really a system service. To make use of this, you have to obtain the service object through getSystemService(NOTIFICATION_SERVICE) out of your activity.
Typically the NotificationManager provides you with three methods.

  • notify() — raise a Notification.
  • cancel() — get rid of an existing Notification.
  • cancelAll() — get rid of all existing Notification.
Notifications Through Hardware
  • You possibly can flash LEDs simply by setting lights to true as wll as giving off/on durations in milliseconds for the light through ledOnMS as well as ledOffMS. We need to give Notification.FLAG_SHOW_LIGHTS as a field for the Notification object in order to enable the LED.
  • You can play a sound, using a Uri.
  • You possibly can vibrate the deviceby providing the on/off patterns for the vibration. To make use of this, you will have to ask for the VIBRATE permission.
  • As an alternative to fine tuning the hardware options on your own, you can even make use of the platform defaults field in the Notification.
For example you can use DEFAULT_LIGHTS to set the factory values for the lights, as well as you can also set DEFAULT_SOUND to get the default value of the sound property, in addition you can set the default vibrate properties by making use of the DEFAULT_VIBRATE, else if you want to use the factory settings for all the property then simply set DEFAULT_ALL.
Icons, Icons, Icons
  • To build an symbol for any Notification, it is advisable to place a couple of public fields icon and in addition contentIntent, in which you provide a PendingIntent to get brought up once the icon is actually clicked.
  • A PendingIntent is really a wrapper of a typical Intent which allows the actual Intent to get called later, through any other process, to begin an activity.
Additionally you can provide a new text blurb to show up as soon as the icon is placed on the actual status bar.
If you want all three, the simpler approach is to call setLatestEventInfo()

You can also set a value in the number public field of your Notification. This will likely trigger the number you actually provide to get drawn above top of the icon within a corner. This is often applied, for instance, showing the amount of unread emails. By default, the number will be ignored and not used.

Creating Layout for Notification Example
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <Button android:id="@+id/notify"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:text="Raise a notification"
      android:onClick="notifyMe"
      />
  <Button android:id="@+id/cancel"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:text="Clear the notification"
      android:onClick="clearNotification"
      />
</LinearLayout>
Java Code for Notification
src/SampleNotificationExampleActivity.java
package com.vimaltuts.android.notificationexample;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SampleNotificationExampleActivity extends Activity {
	  private static final int NOTIFY_ME_ID=1987;
	  private int count=0;
	  private NotificationManager mgr=null;
	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
	    mgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	  }
	  public void notifyMe(View v) {
	    Notification note=new Notification(R.drawable.stat_notify_chat,"Status message!",System.currentTimeMillis());
	    PendingIntent i=PendingIntent.getActivity(this, 0,new Intent(this, NotificationMessage.class),0);
	    note.setLatestEventInfo(this, "New Email","Unread Conversation", i);
	    note.number=++count;
	    note.vibrate=new long[] {500L, 200L, 200L, 500L};
	    note.flags|=Notification.FLAG_AUTO_CANCEL;
	    mgr.notify(NOTIFY_ME_ID, note);
	  }
	  public void clearNotification(View v) {
	    mgr.cancel(NOTIFY_ME_ID);
	  }
	}
src/MySampleNotificationMessage.java
package com.vimaltuts.android.mysamplenotificationexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MySampleNotificationMessage extends Activity {
                 public void onCreate(Bundle savedInstanceState) {
		    super.onCreate(savedInstanceState);
		    TextView txt=new TextView(this);
		    txt.setText("This is the message!");
		    setContentView(txt);
		  }
}
Set Vibrate Permission
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vimaltuts.android.samplenotificationexample"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SampleNotificationExampleActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Output Screen of the Android Custom Font Example


Click “Raise a Notification” Button.




So far we have play with the Android Notification Bar Example through this Android Tutorial for Beginners : Android Notification Bar Example Tutorial.
On the next tutorial we gonna see about the Android XML Parser Example

Leave a Reply

Scroll To Top

Foolow Me on Google Plus

VimalTuts on Twitter
62 people follow VimalTuts
meoluoi_abhijeetLukus_Q7silly_piminkyuuuMillerHeengmomanvedpawar