[Android] Notification usage

Source: Internet
Author: User

 

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 );

}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.