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.widget.Button;
/**
* This class create status bar notification and cancel it
*
*/
public class NotificationExample extends Activity implements OnClickListener{
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
Button start, cancel;
Notification notifyDetails;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_manager);
// Create object object of notification manager
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// set properties to notification object
notifyDetails = new Notification(R.drawable.icon,
"Notification, Click Me!", System.currentTimeMillis());
// Set vibration when notify and assign defaults
long[] vibrate = { 100, 100, 200, 300 };
notifyDetails.vibrate = vibrate;
notifyDetails.defaults = Notification.DEFAULT_ALL;
// create buttons
start = (Button) findViewById(R.id.btnShowNotification);
cancel = (Button) findViewById(R.id.btnClear);
// set buttons listeners
start.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == start){
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Test";
CharSequence contentText = "Get back to Application on clicking me.";
Intent notifyIntent = new Intent(context, NotificationExample.class);
PendingIntent intent = PendingIntent.getActivity(NotificationExample.this, 0,
notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
// Set properties to notify object - its title, text
notifyDetails.setLatestEventInfo(context, contentTitle,
contentText, intent);
// Notify user in status bar
mNotificationManager.notify(SIMPLE_NOTFICATION_ID,
notifyDetails);
}
else if(v == cancel){
// Cancel notification
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
}
}
}
No comments:
Post a Comment