Package com. noti. activity;
Import Android. App. activity;
Import Android. App. notification;
Import Android. App. icationicationmanager;
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;
Import Android. widget. remoteviews;
Public class main extends activity {
/** Called when the activity is first created .*/
Private button BTN; // use the system notification Layout
Private button btn1; // use custom Layout
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
BTN = (button) findviewbyid (R. Id. BTN );
Btn1 = (button) findviewbyid (R. Id. btn1 );
BTN. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Shownotification ();
}
});
Btn1.setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Showmynotification ();
}
});
}
Public void shownotification (){
Obtain the icationicationmanager object of the system.
Icationicationmanager notificationmanager = (notificationmanager) getsystemservice (context. icationication_service );
// Instantiate a notification
Notification = new notification (R. drawable. image1, "notice", system. currenttimemillis ());
Intent intent = new intent (main. This, otheractivity. Class); // click the intent triggered by this notification
Pendingintent = pendingintent. getactivity (this, 0, intent, 1 );
Notification. setlatesteventinfo (this, "title", "a message", pendingintent); // display the layout and trigger of notification
Icationicationmanager. Y (R. Id. BTN, notification); // sends a notification and determines whether it is a new notification based on the ID.
}
Public void showmynotification (){
Icationicationmanager notificationmanager = (notificationmanager) getsystemservice (context. icationication_service );
Notification = new notification (R. drawable. Icon, "notice", system. currenttimemillis ());
// To customize the layout, first define a layout file notification. XML, such as an imageview and textview, and use this layout file to generate a // remoteviews instance.
Remoteviews contentview = new remoteviews (getpackagename (), R. layout. Notification );
Contentview. setimageviewresource (R. Id. Image, R. drawable. image1 );
Contentview. settextviewtext (R. Id. Text, "Hello World ");
// Assign a view to notification
Notification. contentview = contentview;
Notification. contentintent = pendingintent;
// The above code replaces the system's setlatesteventinfo () method
Intent intent = new intent (main. This, otheractivity. Class );
Pendingintent = pendingintent. getactivity (this, 0, intent, 1 );
Icationicationmanager. Notify (R. Id. BTN, notification );
}
}