Android Notification, android
A small demo. Click the send notification button to send the notification to the notification bar of the device. Click Clear notification to clear the Message notification in the notification bar.
Package zhangphil. notification; import android. OS. bundle; import android. view. view; import android. widget. button; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. support. v4.app. notificationCompat; public class MainActivity extends Activity {private final int NOTIFICATION_ID = 105; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button send = (Button) findViewById (R. id. send); send. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {sendNotification () ;}}); Button clear = (Button) findViewById (R. id. clear); clear. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {clearNotification () ;}}) ;}private void sendNotification () {icationicationmanager icationmanager = (icationicationmanager) getSystemService (icationication_service ); icationicationcompat. builder mBuilder = new NotificationCompat. builder (this); mBuilder. setSmallIcon (R. drawable. ic_launcher); mBuilder. setContentTitle ("Notification title"); mBuilder. setContentText ("Notification content"); notification Notification = mBuilder. build (); // The default setting is when the notification is sent to the notification bar: The sound is prompted + the notification is shaken by the mobile phone. defaults = Notification. DEFAULT_SOUND | Notification. DEFAULT_VIBRATE; // notification time. when = System. currentTimeMillis (); // The notification bar sent to the mobile phone icationicationmanager. Y (ication_id _id, notification);} private void clearNotification () {icationicationmanager icationmanager = (icationicationmanager) getSystemService (icationication_service); NotificationManager. cancel (icationication_id );}}
Activity_main.xml
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: id = "@ + id/send" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "send notification"/> <Button android: id = "@ + id/clear" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "clear notification"/> </LinearLayout>