Notification和Notification Manager的使用

  允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://52android.blog.51cto.com/2554429/500661

    当一个广播接收器接收到广播消息,并不能通过可视化的界面来显示广播信息。这里我们可以通过状态提示栏(State Bar)来显示广播信息的内容,图标以及震动等信息。这就需要使用Notification控件和Notification Manager。
    下面以一个实例,来说明状态提示栏的应用。在这个实例中,由广播接收器接收一个收到短信的广播消息,然后开启一个Service,由这个服务将通知信息显示在状态提示栏中。
  广播接收器:

 
 

  1. public class MyReciever extends BroadcastReceiver {  
  2.       
  3.     Intent i;  
  4.           
  5.     @Override 
  6.     public void onReceive(Context context, Intent intent) {  
  7.         // TODO Auto-generated method stub  
  8.         //当广播接收器接收到广播消息时,将开启一个Service
  9. i=new Intent();  
  10.         i.setAction("SERVICE_ACTION");  
  11.    context.startService(i);  
  12.                      
  13.     }  
  14.       
  15.        
  16. }  

  服务:

 
 

  1. public class Myservice extends Service {   
  2.     private NotificationManager mNM;   
  3.     private Notification nF;   
  4.     private Intent i;  
  5.     private PendingIntent pi;  
  6.     @Override   
  7.     public void onCreate() {   
  8.           
  9.     }   
  10.    
  11.     @Override   
  12.     public int onStartCommand(Intent intent, int flags, int startId) {   
  13. //初始化消息管理器对象
  14.   mNM=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  15. //设置当点击通知消息时的转移操作,当用户点击消息时,将转移到After        
  16. i=new Intent();  
  17.         i.setClass(Myservice.this, After.class);  
  18.         pi =PendingIntent.getActivity(Myservice.this0, i, 0);  
  19. // 对通知进行初始化并进行参数进行设置。       
  20. nF=new Notification();  
  21. //设置图标
  22.         nF.icon=R.drawable.icon;
  23. //设置通知内容  
  24.         nF.tickerText="收到通知";
  25. //设置通知栏上显示的信息标题,内容以及点击通知时的转向  
  26.         nF.setLatestEventInfo(this"通知""成功", pi);  
  27. //执行通知        
  28. mNM.notify(0, nF);  
  29.          
  30.         return 0;   
  31.     }   
  32.    
  33.     @Override   
  34.     public void onDestroy() {   
  35.         // Cancel the persistent notification.   
  36.          
  37.    
  38.         // Tell the user we stopped.   
  39.         Toast.makeText(this"destroy", Toast.LENGTH_SHORT).show();   
  40.     }  
  41.  
  42.     @Override 
  43.     public IBinder onBind(Intent intent) {  
  44.         // TODO Auto-generated method stub  
  45.         return null;  
  46.     }   
  47. }  

  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"> 
  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.                 
  13.             </intent-filter> 
  14.         </activity> 
  15. //接收器的部署
  16.         <receiver android:name=".MyReciever"> 
  17.            <intent-filter> 
  18.                <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
  19.                  
  20.            </intent-filter> 
  21.         </receiver>
  22. //对服务的部署 
  23.         <service android:name=".Myservice">                
  24.              <intent-filter> 
  25.                <action android:name="SERVICE_ACTION"/> 
  26.            </intent-filter>          
  27.         </service>   
  28.         <activity android:name=".After" 
  29.                   android:label="an"> 
  30.               
  31.         </activity>     
  32.     </application> 
  33.     <uses-sdk android:minSdkVersion="8" /> 
  34. //设置该应用可以有接收短信的权限    
  35. <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
  36.       
  37.       
  38.        
  39. </manifest>  

    对于Notification和Notification Manager的使用,有以下几点是需要注意的:

    A. 只有Activity和Serviece可以开启通知,其他的组件包括广播接收器并不能直接开启。如果需要对系统广播进行消息提示的话,则需要在广播接收器中转移到Activity或者Service中,由他们开启通知。

    B. 除了上述实例中设置的通知参数之外,还有其他一些参数,例如震动,声音等,具体可以参考SDK文档中的说明。

本文出自 “我的Android开发志” 博客,请务必保留此出处http://52android.blog.51cto.com/2554429/500661

posted on 2011-06-23 15:07  小小博客小小员  阅读(583)  评论(0编辑  收藏  举报

导航