Easy use of Android------Notification notification bar

Source: Internet
Author: User

Notification is the notification displayed in the phone status bar


Create a notification object from the Notification.builder class.

Notification.builder Common methods:

SetDefaults (): Set notification LED lights, music, vibration, etc.

Setautocancle (): Set the status bar to automatically delete notifications after clicking notifications

Setcontenttitle (): Set notification title

Setcontenttext (): Set notification content

Setsmallcon (): Set small icon

Setlargecon (): Set Large icon

Settick (): Set notification in status bar for this

Setcontentintent (): Sets the pendingintent of the program component to be launched after the click Notification

Setwhen (): Set the time for notification publication

Steps:

1. Call the Getsystemservice (Notification_service) method to get the Notificationmanager method of the system

Manager = (Notificationmanager) getsystemservice (Context.notification_service);


2. Create a Notification.builder object

Notification.builder Builder = new Notification.builder (mainactivity.this);


3. Setting various properties for builder

4. Create a Notification object
Notification Notification = Builder.build ();

5. Send notification via Notificationmanager's Notify method
Manager.notify (ID, notification);

Demo:

Activity:

public class Mainactivity extends Activity {Button send, Del; Notificationmanager manager;int ID = 0x123; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); send = (Button) Findviewbyid (r.id.send);d el = (Button) Findviewbyid (r.id.del); manager = (Notificationmanager) getsystemservice (Context.notification_service); Send.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent ( Mainactivity.this, Other.class); pendingintent pi = pendingintent.getactivity (mainactivity.this,0, intent, 0); Notification.builder Builder = new Notification.builder (mainactivity.this); builder//Notification Notification = new// Notification.builder (Mainactivity.this)//Set open notification, the notification cancels. Setautocancel (TRUE)//Set notification prompt information. Setticker ("new Message")// Sets the icon for the notification. Setsmallicon (R.drawable.pig)//sets the caption of the notification. Setcontenttitle ("Not good!!!" ")//Set the content of the notification. Setcontenttext (" Your home Pig ran ")//Set use the system default sound, Led.setdefaults (notification.default_lights| Notification.default_sound)//Set notification release time. Setwhen (System.currenttimemillis ())//Set the activity to start. Setcontentintent (PI). Build (); Notification Notification = Builder.build (); Manager.notify (ID, Notification);}}); Del.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {manager.cancel (ID);}});}}


Click Send notification:


Clicking the notification jumps to another activity:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Easy use of Android------Notification notification bar

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.