RSS

Creating a StatusBar Notification

13 Jul

The following is the code for creating a status bar notification; it also includes a Dialog box, but that’s not in the scope of this blogpost:

import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class StatusBar extends Activity implements OnClickListener {

	Button notify, dialog;
	NotificationManager nm;

	static final int uniqueId = 32125;

	protected void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.status);

		notify = (Button) findViewById(R.id.bNotify);
		dialog = (Button) findViewById(R.id.bDialog);
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		nm.cancel(uniqueId);

		notify.setOnClickListener(this);
		dialog.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch(v.getId()){
		case R.id.bNotify:
			Intent intent = new Intent(this, StatusBar.class);
			PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

			String body = "You have been notified by gilo";
			String title = "gilo";

			Notification n = new Notification(R.drawable.ic_launcher,body, System.currentTimeMillis());
			n.setLatestEventInfo(this, title, body, pi);
			n.defaults=Notification.DEFAULT_ALL;
			nm.notify(uniqueId, n);
			finish();
			break;
		case R.id.bDialog:
			Dialog d =new Dialog(this);
			d.setTitle("Oh Yeah!!");
			TextView tv =new TextView(this);
			tv.setText("This is a dialog");

			d.setContentView(tv);
			d.show();
			break;
		}
	}
}
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

 
%d bloggers like this: