Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android提高21篇之六:BroadcastReceiver

Android提高21篇之六:BroadcastReceiver

編輯:關於android開發

       前面分別討論了Activity和Service,這次就輪到BroastcastReceiver,Broastcast是應用程序間通信的手段。BroastcastReceiver也是跟Intent緊密相連的,動態/靜態注冊了BroastcastReceiver之後,使用sendBroadcast把Intent發送之後,系統會自動把符合條件的BroastcastReceiver啟動,跟嵌入式系統的中斷類似。

       本文主要演示了如何靜態/動態注冊BroastcastReceiver,向系統索取電量信息,以及枚舉信息的字段。本文運行截圖如下:

Android提高21篇之五:BroadcastReceiver

Android提高21篇之五:BroadcastReceiver

       上圖是發送Intent至內部動態注冊的BroadcastReceiver,接收到之後顯示消息名稱。動態注冊BroadcastReceiver用到registerReceiver()。

Android提高21篇之五:BroadcastReceiver

       上圖是發送Intent至內部靜態注冊的BroadcastReceiver,接收到之後顯示消息名稱。靜態注冊比動態注冊麻煩點,先新建一個類繼承BroadcastReceiver,然後到AndroidManifest.xml 添加

XML/HTML代碼
  1. <receiver android:name="clsReceiver2">  
  2.      <intent-filter>  
  3.           <action android:name="com.testBroadcastReceiver.Internal_2"/>  
  4.      </intent-filter>  
  5. </receiver>  

       第一個name是類名,第二個是action的名稱。

Android提高21篇之五:BroadcastReceiver

       上圖是枚舉Intent消息的字段,這個功能比較適合懶人,把收到的Intent消息的字段全部分解了,再看看哪個需要的,懶得記住。實現這部分的代碼如下:

Java代碼
  1. //當未知Intent包含的內容,則需要通過以下方法來列舉  
  2. Bundle b=intent.getExtras();  
  3. Object[] lstName=b.keySet().toArray();  
  4.   
  5. for(int i=0;i<lstname.length;i++)  
  6. {  
  7.     String keyName=lstName[i].toString();                                   
  8.     Log.e(keyName,String.valueOf(b.get(keyName)));  
  9. }  

       main.xml的代碼如下:

XML/HTML代碼
  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"   
  2.         android:orientation="vertical" android:layout_width="fill_parent"  
  3.         android:layout_height="fill_parent">  
  4.   
  5.         <button android:id="@+id/Button01" android:layout_width="wrap_content"   
  6.                 android:layout_height="wrap_content" android:text="發送至內部動態注冊的BroadcastReceiver">  
  7.         <button android:id="@+id/Button02" android:layout_width="wrap_content"   
  8.                 android:layout_height="wrap_content" android:text="發送至內部靜態注冊BroadcastReceiver">  
  9.         <button android:id="@+id/Button03" android:layout_width="wrap_content"   
  10.                 android:layout_height="wrap_content" android:text="發送至系統BroadcastReceiver">  

       testBroadcastReceiver.java的代碼如下:

Java代碼
  1. package com.testBroadcastReceiver;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.Toast;  
  13.   
  14. public class testBroadcastReceiver extends Activity {  
  15.     Button btnInternal1,btnInternal2,btnSystem;  
  16.     static final String INTENAL_ACTION_1 = "com.testBroadcastReceiver.Internal_1";  
  17.     static final String INTENAL_ACTION_2 = "com.testBroadcastReceiver.Internal_2";  
  18.     static final String INTENAL_ACTION_3 = "com.testBroadcastReceiver.Internal_3";  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         btnInternal1=(Button)this.findViewById(R.id.Button01);  
  24.         btnInternal1.setOnClickListener(new ClickEvent());  
  25.         btnInternal2=(Button)this.findViewById(R.id.Button02);  
  26.         btnInternal2.setOnClickListener(new ClickEvent());  
  27.         btnSystem=(Button)this.findViewById(R.id.Button03);  
  28.         btnSystem.setOnClickListener(new ClickEvent());  
  29.         //動態注冊廣播消息  
  30.                 registerReceiver(bcrIntenal1, new IntentFilter(INTENAL_ACTION_1));  
  31.     }  
  32.     class ClickEvent implements View.OnClickListener{  
  33.   
  34.                 @Override  
  35.                 public void onClick(View v) {  
  36.                         if(v==btnInternal1)//給動態注冊的BroadcastReceiver發送數據  
  37.                         {  
  38.                                 Intent intent = new Intent(INTENAL_ACTION_1);  
  39.                                 sendBroadcast(intent);  
  40.                         }  
  41.                         else if(v==btnInternal2)//給靜態注冊的BroadcastReceiver發送數據  
  42.                         {  
  43.                                 Intent intent = new Intent(INTENAL_ACTION_2);  
  44.                                 sendBroadcast(intent);  
  45.                         }  
  46.                         else if(v==btnSystem)//動態注冊 接收2組信息的BroadcastReceiver  
  47.                         {  
  48.                                 IntentFilter filter = new IntentFilter();//  
  49.                         filter.addAction(Intent.ACTION_BATTERY_CHANGED);//系統電量檢測信息  
  50.                         filter.addAction(INTENAL_ACTION_3);//第三組自定義消息  
  51.                                 registerReceiver(batInfoReceiver, filter);  
  52.                                   
  53.                                 Intent intent = new Intent(INTENAL_ACTION_3);  
  54.                                 intent.putExtra("Name", "hellogv");  
  55.                                 intent.putExtra("Blog", "http://blog.csdn.net/hellogv");  
  56.                                 sendBroadcast(intent);//傳遞過去  
  57.                         }  
  58.                 }  
  59.               
  60.     }  
  61.       
  62.     /* 
  63.      * 接收動態注冊廣播的BroadcastReceiver 
  64.      */  
  65.         private BroadcastReceiver bcrIntenal1 = new BroadcastReceiver() {  
  66.                   
  67.                 public void onReceive(Context context, Intent intent) {  
  68.                         String action = intent.getAction();  
  69.                         Toast.makeText(context, "動態:"+action, 1000).show();  
  70.                 }  
  71.         };  
  72.           
  73.   
  74.         private BroadcastReceiver batInfoReceiver = new BroadcastReceiver() {  
  75.                   
  76.                 public void onReceive(Context context, Intent intent) {  
  77.                         String action = intent.getAction();  
  78.                         //如果捕捉到的action是ACTION_BATTERY_CHANGED  
  79.                         if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {  
  80.                                 //當未知Intent包含的內容,則需要通過以下方法來列舉  
  81.                                 Bundle b=intent.getExtras();  
  82.                                 Object[] lstName=b.keySet().toArray();  
  83.   
  84.                                 for(int i=0;i<lstname.length;i++)  
  85.                                 {  
  86.                                         String keyName=lstName[i].toString();  
  87.                                         Log.e(keyName,String.valueOf(b.get(keyName)));  
  88.                                 }  
  89.                         }  
  90.                         //如果捕捉到的action是INTENAL_ACTION_3  
  91.                         if (INTENAL_ACTION_3.equals(action)) {  
  92.                                 //當未知Intent包含的內容,則需要通過以下方法來列舉  
  93.                                 Bundle b=intent.getExtras();  
  94.                                 Object[] lstName=b.keySet().toArray();  
  95.   
  96.                                 for(int i=0;i<lstname.length;i++)  
  97.                                 {  
  98.                                         String keyName=lstName[i].toString();  
  99.                                         Log.e(keyName,b.getString(keyName));  
  100.                                 }  
  101.                         }  
  102.                 }  
  103.         };  
  104. }  

       clsReceiver2.java的代碼如下:

Java代碼
  1. package com.testBroadcastReceiver;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.widget.Toast;  
  7.   
  8. /* 
  9. * 接收靜態注冊廣播的BroadcastReceiver, 
  10. * step1:要到AndroidManifest.xml這裡注冊消息 
  11. *                  
  12.   <action 
  13.       android:name="com.testBroadcastReceiver.Internal_2"/> 
  14.   </action 
  15.  
  16.   step2:定義消息的字符串 
  17.   step3:通過Intent傳遞消息來驅使BroadcastReceiver觸發 
  18. */  
  19. public class clsReceiver2 extends BroadcastReceiver{  
  20.         @Override  
  21.         public void onReceive(Context context, Intent intent) {  
  22.                 String action = intent.getAction();  
  23.                 Toast.makeText(context, "靜態:"+action, 1000).show();  
  24.                   
  25.         }  
  26. }  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved