Thursday 22 March 2012

Android Notification Example


import java.sql.Date;

import android.app.Activity;
import android.os.Bundle;

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;

public class AndroidNotification extends Activity {

NotificationManager myNotificationManager;
private static final int NOTIFICATION_ID = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button myGen = (Button) findViewById(R.id.gen);
myGen.setOnClickListener(myGenOnClickListener);
Button myClear = (Button) findViewById(R.id.clear);
myClear.setOnClickListener(myClearOnClickListener);



}

private void GeneratNotification() {


myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

CharSequence NotificationTicket = new Date(System.currentTimeMillis()).toLocaleString();

CharSequence NotificationContent = "- Notification is coming -";
long when = System.currentTimeMillis();

Notification notification = new Notification(
android.R.drawable.ic_lock_idle_charging, NotificationTicket, when);


Context context = getApplicationContext();

Intent notificationIntent = new Intent(this, AndroidNotification.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

notification.setLatestEventInfo(context, null,NotificationContent, contentIntent);


myNotificationManager.notify(NOTIFICATION_ID, notification);

}

Button.OnClickListener myGenOnClickListener = new Button.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GeneratNotification();
}

};

Button.OnClickListener myClearOnClickListener = new Button.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myNotificationManager.cancel(NOTIFICATION_ID);
}

};

class myNotificaton extends Notification{

}
}

3 comments: