前面分別討論了html" target=_blank>Activity和Service,這次就輪到BroastcastReceiver,Broastcast是應用程式間通訊的手段。BroastcastReceiver也是跟Intent緊密相連的,動態/靜態註冊了BroastcastReceiver之後,使用sendBroadcast把Intent發送之後,系統會自動把合格BroastcastReceiver啟動,跟嵌入式系統的中斷類似。
本文主要示範了如何靜態/動態註冊BroastcastReceiver,向系統索取電量資訊,以及枚舉資訊的欄位。本文運行如下:
是發送Intent至內部動態註冊的BroadcastReceiver,接收到之後顯示訊息名稱。動態註冊BroadcastReceiver用到registerReceiver()。
是發送Intent至內部靜態註冊的BroadcastReceiver,接收到之後顯示訊息名稱。靜態註冊比動態註冊麻煩點,先建立一個類繼承BroadcastReceiver,然後到AndroidManifest.xml 添加
view plaincopy to clipboardprint?
<receiver android:name="clsReceiver2">
<intent-filter>
<action
android:name="com.testBroadcastReceiver.Internal_2"/>
</intent-filter>
</receiver>
<receiver android:name="clsReceiver2">
<intent-filter>
<action
android:name="com.testBroadcastReceiver.Internal_2"/>
</intent-filter>
</receiver>
第一個name是類名,第二個是action的名稱。
是枚舉Intent訊息的欄位,這個功能比較適合懶人,把收到的Intent訊息的欄位全部分解了,再看看哪個需要的,懶得記住。實現這部分的代碼如下:
view plaincopy to clipboardprint?
//當未知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)));
}
//當未知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的代碼如下:
view plaincopy to clipboardprint?
<?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>
<?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的代碼如下:
view plaincopy to clipboardprint?
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);
btnIn