Sunday, May 10, 2009

How to Send Message to Status Bar in Android

Status bar is very important part of android interface. It alerts the user about new sms, emails and many other stuff. Applications can also send messages or notifications to status bar for user. In this post, I will guide the user how to send messages to status bar using a simple application.
Application has a simple interface which has a edit box and a button. User will type any text in edit box and will click on button. which will send the message to status bar. Given below is the code for interface:

package org.engineers.android;

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.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

/**
*
* @author amer sohail
*/
public class NotificationActivity extends Activity {

private EditText editor;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
setContentView(getEditor());
}

private LinearLayout getEditor(){
LinearLayout parent = new LinearLayout(this);
parent.setOrientation(LinearLayout.VERTICAL);
editor = new EditText(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.5f);
parent.addView(editor ,lp);

LinearLayout actionBar = new LinearLayout(this);
actionBar.setOrientation(LinearLayout.HORIZONTAL);
Button sendButton = new Button(this);
sendButton.setText("Send");
sendButton.setOnClickListener(listener);
actionBar.addView(sendButton);
parent.addView(actionBar,lp);
return parent;
}

private OnClickListener listener = new OnClickListener() {

public void onClick(View arg0) {

//write code here
}
};

}

Above code is very simple. There is an activity named as "NotificationActivity" extended from Activity class. This will be the view of application. Inside "onCreate" method, content view is set which is developed in separate method "getEditor". Although good approach to write interface is in xml but i wrote it here using code because this was a very simple application. I used Linear Layout with vertical orientation as main layout. It contains EditBox and Button vertically. A OnClickListener was also set to button's OnClickListener. OnClickListener contains a method onClick where i will write main code to sent message to status bar. Code for that is given below:


NotificationManager manager= (NotificationManager)NotificationActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.alert_dialog_icon,null, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(NotificationActivity.this, 0,
new Intent(NotificationActivity.this, NotificationActivity.class), 0);
notification.setLatestEventInfo(NotificationActivity.this, "Alert", editor.getText(), contentIntent);
manager.notify(1, notification);

NotificationManager is main class which is used to send messages in the form of Notification to the status bar. NotificationManager is gotten from Content.getSystemService method. Since Acitivity is extended from Context class, so i used "NotificationActivity" to get NotificationManager. Next step is to create a Notification. It has many constructors but constructor which i have used contains three parameters. First one is "icon" to be displayed in the notification. second one is message which i will set later and third one is time in milliseconds of generating the message. Notification object also requires a PendingIntent to display acitivity when user will click on message in status bar. PendingIntent.getActivity is used to create PendingIntent for activity. In the last, i call notify of NotificationManager which takes two parameter, first one is unique identifier for notification and second one is Notification itself. That's it. You can send notification to status bar in four simple lines. Create a project and try it. I will post another blog to explain PendingIntent in details.

No comments: