Using Notification Bar – Android tutorial

In this tutorial we are going to learn how to use the Notification Bar: This is very useful, and is used almost in every application that runs in the background, or that is activates by events. In this example we will add a button that when pressed will open send a notification to the notification bar, with Title and Text, and will also make the Led of the phone blink with a purple color.
  • Add notification Title
  • Add notification Text
  • Add Picture to the notification
  • Make the Trackball Led Blink (For telephone that have the SW only)
  • Chane the color of the blinking Led
In this learn by example, most of the explanation will be inside the code. Lets Start: We will start by adding the Button we are going to use to send a notification to the phone.


<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/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Notification" />
</linearlayout>

 
The next step is to add some code behind the button, so it does what we want in the UsingNotificationBar.java file we add in the onCreate function the fallowing:
public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button1Action Button1Obj    = new Button1Action();
        Button1               = (Button) findViewById(R.id.Button1);
        Button1.setOnClickListener(Button1Obj);

    }/* End of onCreate Method */
 
We now add the class that takes care of the notification. To send a notification we will need to add an intent, with the text we want to add to the notification Inside the UsingNotificationBar class we need to add the fallowing class:
public class Button1Action implements Button.OnClickListener
    {
      @Override
      public void onClick(View arg0)
      {
         /* Get the notification manager  */
         mNotManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

         /* Create a notification */
         String MyText = "Text inside: By Firstdroid.com";
         mNotification = new Notification(
                R.drawable.icon,                // An Icon to display
                MyText,                         // the text to display in the ticker
                System.currentTimeMillis()       ); // the time for the notification

         /* Starting an intent */
         String MyNotifyTitle = "Firstdroid Rocks!!!";
         String MyNotifiyText  = "Firstdroid: our forum at www.firstdroid.com";
         Intent MyIntent = new Intent( getApplicationContext(), UsingNotificationBar.class );
         MyIntent.putExtra("extendedTitle", MyNotifyTitle);
         MyIntent.putExtra("extendedText" , MyNotifiyText);
         PendingIntent StartIntent = PendingIntent.getActivity(  getApplicationContext(),
                                                   0,
                                                   MyIntent,
                                                   0);

         /* Set notification message */
         mNotification.setLatestEventInfo(   getApplicationContext(),
                                    MyNotifyTitle,
                                    MyNotifiyText,
                                    StartIntent);

         mNotification.ledOnMS  = 200;    //Set led blink (Off in ms)
         mNotification.ledOffMS = 200;    //Set led blink (Off in ms)
         mNotification.ledARGB = 0x9400d3;   //Set led color

         /* Sent Notification to notification bar */
         mNotManager.notify(  NotificationId , mNotification );            

      }/* End of method onClick */

    }/* End of Class Button2Action_CDT */
 
Also, do not forget the imports we need:
package firstdroid.tutorial.UsingNotificationBar;

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;
import android.widget.Button;
 
That is all, now run the code and you should see the fallowing: Application as it startsGet the notificationreceiving the notification Any Question, Comment or Remark please refer to our forum: www.firstdroid.com forum Enjoy, Firstdroid.