Use of notification and notification Manager

Source: Internet
Author: User
Tags: Android notification can be reproduced. During reprinting, you must mark the original source, author information, and this statement in the form of a hyperlink. Otherwise, legal liability will be held. Http://52android.blog.51cto.com/2554429/500661

When a broadcast receiver receives a broadcast message, it cannot display the broadcast information through a visual interface. Here, we can use the state bar to display the broadcast information content, icons, vibrations, and other information. This requires the notification control and notification manager.
The following uses an instance to describe the application in the status prompt bar. In this instance, the broadcast receiver receives a broadcast message that receives the short message, and then enables a service, which displays the notification information in the status prompt bar.
Broadcast receiver:

 
 

     
     
  1. Public class myreciever extends broadcastreceiver {
  2. Intent I;
  3. @ Override
  4. Public void onreceive (context, intent ){
  5. // Todo auto-generated method stub
  6. // When the broadcast receiver receives a broadcast message, a service is enabled.
  7. I = new intent ();
  8. I. setaction ("service_action ");
  9. Context. startservice (I );
  10. }
  11. }

Service:

 
 

     
     
  1. Public class myservice extends Service {
  2. Private icationicationmanager mnm;
  3. Private notification NF;
  4. Private intent I;
  5. Private pendingintent PI;
  6. @ Override
  7. Public void oncreate (){
  8. }
  9. @ Override
  10. Public int onstartcommand (intent, int flags, int startid ){
  11. // Initialize the message manager object
  12. Mnm = (icationicationmanager) getsystemservice (notification_service );
  13. // Set the transfer operation when a notification message is clicked. When a user clicks a message, it is transferred to after
  14. I = new intent ();
  15. I. setclass (myservice. This, after. Class );
  16. Pi = pendingintent. getactivity (myservice. This, 0, I, 0 );
  17. // Initialize the notification and set parameters.
  18. NF = new notification ();
  19. // Set the icon
  20. NF. Icon = R. drawable. Icon;
  21. // Set the notification content
  22. NF. tickertext = "receive notification ";
  23. // Set the title and content of the information displayed on the notification bar and the redirection when the notification is clicked.
  24. NF. setlatesteventinfo (this, "notification", "success", Pi );
  25. // Execution Notification
  26. Mnm. 127y (0, NF );
  27. Return 0;
  28. }
  29. @ Override
  30. Public void ondestroy (){
  31. // Cancel the persistent notification.
  32. // Tell the user we stopped.
  33. Toast. maketext (this, "Destroy", Toast. length_short). Show ();
  34. }
  35.  
  36. @ Override
  37. Public ibinder onbind (intent ){
  38. // Todo auto-generated method stub
  39. Return NULL;
  40. }
  41. }

Androidmanifest. xml

 
 

     
     
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. bt"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name" Android: debuggable = "true">
  7. <Activity Android: Name = ". broadcasttest"
  8. Android: Label = "@ string/app_name">
  9. <Intent-filter>
  10. <Action Android: Name = "android. Intent. Action. Main"/>
  11. <Category Android: Name = "android. Intent. Category. launcher"/>
  12. </Intent-filter>
  13. </Activity>
  14. // Receiver deployment
  15. <Cycler Android: Name = ". myreciever">
  16. <Intent-filter>
  17. <Action Android: Name = "android. provider. telephony. sms_received"/>
  18. </Intent-filter>
  19. </Cycler>
  20. // Service deployment 
  21. <Service android: Name = ". myservice">
  22. <Intent-filter>
  23. <Action Android: Name = "service_action"/>
  24. </Intent-filter>
  25. </Service>
  26. <Activity Android: Name = ". After"
  27. Android: Label = "an">
  28. </Activity>
  29. </Application>
  30. <Uses-SDK Android: minsdkversion = "8"/>
  31. // Set the application to have the permission to receive text messages.
  32. <Uses-Permission Android: Name = "android. Permission. receive_sms"> </uses-Permission>
  33. </Manifest>

Note the following when using notification and notification Manager:

A. Only activity and serviece can enable notifications. Other components, including broadcast receivers, cannot be enabled directly. To send a message to the system broadcast, you need to transfer the message to activity or service in the broadcast receiver to enable the notification.

B. In addition to the notification parameters set in the above example, there are other parameters, such as vibration and sound. For details, refer to the instructions in the SDK documentation.

This article from "My android development blog", please be sure to keep this source http://52android.blog.51cto.com/2554429/500661

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.