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:
- Public class myreciever extends broadcastreceiver {
- Intent I;
- @ Override
- Public void onreceive (context, intent ){
- // Todo auto-generated method stub
- // When the broadcast receiver receives a broadcast message, a service is enabled.
- I = new intent ();
- I. setaction ("service_action ");
- Context. startservice (I );
- }
- }
|
Service:
- Public class myservice extends Service {
- Private icationicationmanager mnm;
- Private notification NF;
- Private intent I;
- Private pendingintent PI;
- @ Override
- Public void oncreate (){
- }
- @ Override
- Public int onstartcommand (intent, int flags, int startid ){
- // Initialize the message manager object
- Mnm = (icationicationmanager) getsystemservice (notification_service );
- // Set the transfer operation when a notification message is clicked. When a user clicks a message, it is transferred to after
- I = new intent ();
- I. setclass (myservice. This, after. Class );
- Pi = pendingintent. getactivity (myservice. This, 0, I, 0 );
- // Initialize the notification and set parameters.
- NF = new notification ();
- // Set the icon
- NF. Icon = R. drawable. Icon;
- // Set the notification content
- NF. tickertext = "receive notification ";
- // Set the title and content of the information displayed on the notification bar and the redirection when the notification is clicked.
- NF. setlatesteventinfo (this, "notification", "success", Pi );
- // Execution Notification
- Mnm. 127y (0, NF );
- Return 0;
- }
- @ Override
- Public void ondestroy (){
- // Cancel the persistent notification.
- // Tell the user we stopped.
- Toast. maketext (this, "Destroy", Toast. length_short). Show ();
- }
-
- @ Override
- Public ibinder onbind (intent ){
- // Todo auto-generated method stub
- Return NULL;
- }
- }
|
Androidmanifest. xml
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
- Package = "com. bt"
- Android: versioncode = "1"
- Android: versionname = "1.0" type = "codeph" text = "/codeph">
- <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name" Android: debuggable = "true">
- <Activity Android: Name = ". broadcasttest"
- Android: Label = "@ string/app_name">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. Main"/>
- <Category Android: Name = "android. Intent. Category. launcher"/>
- </Intent-filter>
- </Activity>
- // Receiver deployment
- <Cycler Android: Name = ". myreciever">
- <Intent-filter>
- <Action Android: Name = "android. provider. telephony. sms_received"/>
- </Intent-filter>
- </Cycler>
- // Service deployment
- <Service android: Name = ". myservice">
- <Intent-filter>
- <Action Android: Name = "service_action"/>
- </Intent-filter>
- </Service>
- <Activity Android: Name = ". After"
- Android: Label = "an">
- </Activity>
- </Application>
- <Uses-SDK Android: minsdkversion = "8"/>
- // Set the application to have the permission to receive text messages.
- <Uses-Permission Android: Name = "android. Permission. receive_sms"> </uses-Permission>
- </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