Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android提高之BroadcastReceiver實例詳解

Android提高之BroadcastReceiver實例詳解

編輯:Android開發實例

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

本文所示實例代碼主要演示了如何靜態/動態注冊BroastcastReceiver,向系統索取電量信息,以及枚舉信息的字段等功能和。

程序運行截圖如下所示:

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

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

<receiver android:name="clsReceiver2">
 <intent-filter>
 <action
  android:name="com.testBroadcastReceiver.Internal_2"/>
 </intent-filter>
</receiver>

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

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

//當未知Intent包含的內容,則需要通過以下方法來列舉
Bundle b=intent.getExtras();
Object[] lstName=b.keySet().toArray();

for(int i=0;i<lstName.length;i++)
{
 String keyName=lstName[i].toString();
 Log.e(keyName,String.valueOf(b.get(keyName)));
}

main.xml的代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <Button android:id="@+id/Button01" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:text="發送至內部動態注冊的BroadcastReceiver"></Button>
 <Button android:id="@+id/Button02" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:text="發送至內部靜態注冊BroadcastReceiver"></Button>
 <Button android:id="@+id/Button03" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:text="發送至系統BroadcastReceiver"></Button>
</LinearLayout>

testBroadcastReceiver.java的代碼如下:

package com.testBroadcastReceiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class testBroadcastReceiver extends Activity {
  Button btnInternal1,btnInternal2,btnSystem;
  static final String INTENAL_ACTION_1 = "com.testBroadcastReceiver.Internal_1";
  static final String INTENAL_ACTION_2 = "com.testBroadcastReceiver.Internal_2";
  static final String INTENAL_ACTION_3 = "com.testBroadcastReceiver.Internal_3";
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnInternal1=(Button)this.findViewById(R.id.Button01);
    btnInternal1.setOnClickListener(new ClickEvent());
    btnInternal2=(Button)this.findViewById(R.id.Button02);
    btnInternal2.setOnClickListener(new ClickEvent());
    btnSystem=(Button)this.findViewById(R.id.Button03);
    btnSystem.setOnClickListener(new ClickEvent());
    //動態注冊廣播消息
 registerReceiver(bcrIntenal1, new IntentFilter(INTENAL_ACTION_1));
  }
  class ClickEvent implements View.OnClickListener{

 @Override
 public void onClick(View v) {
  if(v==btnInternal1)//給動態注冊的BroadcastReceiver發送數據
  {
  Intent intent = new Intent(INTENAL_ACTION_1);
  sendBroadcast(intent);
  }
  else if(v==btnInternal2)//給靜態注冊的BroadcastReceiver發送數據
  {
  Intent intent = new Intent(INTENAL_ACTION_2);
  sendBroadcast(intent);
  }
  else if(v==btnSystem)//動態注冊 接收2組信息的BroadcastReceiver
  {
  IntentFilter filter = new IntentFilter();//
     filter.addAction(Intent.ACTION_BATTERY_CHANGED);//系統電量檢測信息
     filter.addAction(INTENAL_ACTION_3);//第三組自定義消息
  registerReceiver(batInfoReceiver, filter);
  
  Intent intent = new Intent(INTENAL_ACTION_3);
  intent.putExtra("Name", "hellogv");
  intent.putExtra("Blog", "http://blog.csdn.net/hellogv");
  sendBroadcast(intent);//傳遞過去
  }
 }
   
  }
  /*
   * 接收動態注冊廣播的BroadcastReceiver
   */
 private BroadcastReceiver bcrIntenal1 = new BroadcastReceiver() {
 
 public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  Toast.makeText(context, "動態:"+action, 1000).show();
 }
 };
 private BroadcastReceiver batInfoReceiver = new BroadcastReceiver() {
 public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  //如果捕捉到的action是ACTION_BATTERY_CHANGED
  if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
  //當未知Intent包含的內容,則需要通過以下方法來列舉
  Bundle b=intent.getExtras();
  Object[] lstName=b.keySet().toArray();

  for(int i=0;i<lstName.length;i++)
  {
   String keyName=lstName[i].toString();
   Log.e(keyName,String.valueOf(b.get(keyName)));
  }
  }
  //如果捕捉到的action是INTENAL_ACTION_3
  if (INTENAL_ACTION_3.equals(action)) {
  //當未知Intent包含的內容,則需要通過以下方法來列舉
  Bundle b=intent.getExtras();
  Object[] lstName=b.keySet().toArray();
  for(int i=0;i<lstName.length;i++)
  {
   String keyName=lstName[i].toString();
   Log.e(keyName,b.getString(keyName));
  }
  }
 }
 };
}

clsReceiver2.java的代碼如下:

package com.testBroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/*
 * 接收靜態注冊廣播的BroadcastReceiver,
 * step1:要到AndroidManifest.xml這裡注冊消息
 *  <receiver android:name="clsReceiver2">
  <intent-filter>
  <action
   android:name="com.testBroadcastReceiver.Internal_2"/>
  </intent-filter>
 </receiver>
 step2:定義消息的字符串
 step3:通過Intent傳遞消息來驅使BroadcastReceiver觸發
 */
public class clsReceiver2 extends BroadcastReceiver{
 @Override
 public void onReceive(Context context, Intent intent) {
 String action = intent.getAction();
 Toast.makeText(context, "靜態:"+action, 1000).show();
 }
}

感興趣的朋友可以調試運行該實例,希望能夠對大家的Android項目開發起到一點幫助作用。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved