NotificationおよびNotification Managerの使用


ブロードキャスト受信機がブロードキャストメッセージを受信すると、可視化されたインタフェースを介してブロードキャスト情報を表示することはできない.ここでは,状態提示欄(State Bar)により,放送情報の内容,アイコン,振動などの情報を表示することができる.これは、NotificationコントロールとNotification Managerを使用する必要があります.次に、ステータスプロンプトバーの適用例を説明します.この例では、ブロードキャスト受信機によってメールを受信したブロードキャストメッセージが受信され、サービスがステータスプロンプトバーに通知情報を表示するサービスが開始される.ブロードキャスト受信機:
   

      
      
      
      
  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とServicesのみが通知を開くことができ、他のコンポーネントがブロードキャスト受信機を含む場合、直接開くことはできません.システムブロードキャストにメッセージを提示する必要がある場合は、ブロードキャスト受信機でActivityまたはServiceに移行し、通知を開く必要があります.
B.上記の例で設定した通知パラメータに加えて、振動、音声などの他のパラメータがあり、具体的にはSDKドキュメントの説明を参照してください.