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: