Android --- 60 --- simple use of the Notification bar, androidnotification

Source: Internet
Author: User

Android --- 60 --- simple use of the Notification bar, androidnotification

Notification is a Notification displayed in the mobile phone status bar.


Create a Notification object using the Notification. Builder class.

Common Notification. Builder methods:

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

SetAutoCancle (): sets the notification to be deleted automatically in the status bar after clicking the notification.

SetContentTitle (): Set the notification title

SetContentText (): Set the notification content

SetSmallcon (): Set the small icon

SetLargecon (): Set the big icon

SetTick (): Set the notification prompt in the status bar to this

SetContentIntent (): Set the PendingIntent corresponding to the program component to be started after clicking the notification

SetWhen (): Set the notification release time

 

Steps:

1. Call the getSystemService (icationication_service) method to obtain the NotificationManager method of the system.

Manager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE );


2. Create a Notification. Builder object

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


3. Set various attributes for builder

4. Create a Notification object
Notification notification = builder. build ();

5. send Notification through icationicationmanager's notify y Method
Manager. Policy (ID, notification );

 

Demo:

 

Activity:

Public class MainActivity extends Activity {Button send, del; icationicationmanager manager; int ID = 0x123; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); send = (Button) findViewById (R. id. send); del = (Button) findViewById (R. id. del); manager = (icationicationmanager) 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) // sets the open notification, which is canceled. setAutoCan Cel (true) // sets the notification prompt. setTicker ("New message") // sets the notification icon. setSmallIcon (R. drawable. pig) // set the notification title. setContentTitle ("no good !!! ") // Set the notification content. setContentText ("your pig has run") // you can use the default system sound and LED. setDefaults (Notification. DEFAULT_LIGHTS | Notification. DEFAULT_SOUND) // set the notification release time. setWhen (System. currentTimeMillis () // sets the activity to be started. setContentIntent (pi ). build (); Notification notification = builder. build (); manager. Y (ID, notification) ;}}); del. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {manager. cancel (ID );}});}}


 

 

 

Click send notification:

 


 

 

Click this notification to go to another activity:

 

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.