Android Notification, android

Source: Internet
Author: User

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>

 

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.