Notification和Notification Manager的使用

    當一個廣播接收器接收到廣播消息,並不能經過可視化的界面來顯示廣播信息。這裏咱們能夠經過狀態提示欄(State Bar)來顯示廣播信息的內容,圖標以及震動等信息。這就須要使用Notification控件和Notification Manager。
    下面以一個實例,來講明狀態提示欄的應用。在這個實例中,由廣播接收器接收一個收到短信的廣播消息,而後開啓一個Service,由這個服務將通知信息顯示在狀態提示欄中。
  廣播接收器:android

 
 

      
      
               
      
      
  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. }  

  服務:app

 
 

      
      
               
      
      
  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的使用,有如下幾點是須要注意的:ide

    A. 只有Activity和Serviece能夠開啓通知,其餘的組件包括廣播接收器並不能直接開啓。若是須要對系統廣播進行消息提示的話,則須要在廣播接收器中轉移到Activity或者Service中,由他們開啓通知。this

    B. 除了上述實例中設置的通知參數以外,還有其餘一些參數,例如震動,聲音等,具體能夠參考SDK文檔中的說明。spa

相關文章
相關標籤/搜索