Android提高之BroadcastReceiver實(shí)例詳解
前面幾篇文章分別討論了Activity和Service,本文就來討論BroastcastReceiver,Broastcast是應(yīng)用程序間通信的手段。BroastcastReceiver也是跟Intent緊密相連的,動(dòng)態(tài)/靜態(tài)注冊(cè)了BroastcastReceiver之后,使用sendBroadcast把Intent發(fā)送之后,系統(tǒng)會(huì)自動(dòng)把符合條件的BroastcastReceiver啟動(dòng),這和嵌入式系統(tǒng)的中斷類似。
本文所示實(shí)例代碼主要演示了如何靜態(tài)/動(dòng)態(tài)注冊(cè)BroastcastReceiver,向系統(tǒng)索取電量信息,以及枚舉信息的字段等功能和。
程序運(yùn)行截圖如下所示:


上圖是發(fā)送Intent至內(nèi)部動(dòng)態(tài)注冊(cè)的BroadcastReceiver,接收到之后顯示消息名稱。動(dòng)態(tài)注冊(cè)BroadcastReceiver用到registerReceiver()。

上圖是發(fā)送Intent至內(nèi)部靜態(tài)注冊(cè)的BroadcastReceiver,接收到之后顯示消息名稱。靜態(tài)注冊(cè)比動(dòng)態(tài)注冊(cè)麻煩點(diǎn),先新建一個(gè)類繼承BroadcastReceiver,然后到AndroidManifest.xml 添加
<receiver android:name="clsReceiver2"> <intent-filter> <action android:name="com.testBroadcastReceiver.Internal_2"/> </intent-filter> </receiver>
第一個(gè)name是類名,第二個(gè)是action的名稱。

上圖是枚舉Intent消息的字段,這個(gè)功能比較適合懶人,把收到的Intent消息的字段全部分解了,再看看哪個(gè)需要的,懶得記住。實(shí)現(xiàn)這部分的代碼如下:
//當(dāng)未知Intent包含的內(nèi)容,則需要通過以下方法來列舉
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="發(fā)送至內(nèi)部動(dòng)態(tài)注冊(cè)的BroadcastReceiver"></Button> <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發(fā)送至內(nèi)部靜態(tài)注冊(cè)BroadcastReceiver"></Button> <Button android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發(fā)送至系統(tǒng)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());
//動(dòng)態(tài)注冊(cè)廣播消息
registerReceiver(bcrIntenal1, new IntentFilter(INTENAL_ACTION_1));
}
class ClickEvent implements View.OnClickListener{
@Override
public void onClick(View v) {
if(v==btnInternal1)//給動(dòng)態(tài)注冊(cè)的BroadcastReceiver發(fā)送數(shù)據(jù)
{
Intent intent = new Intent(INTENAL_ACTION_1);
sendBroadcast(intent);
}
else if(v==btnInternal2)//給靜態(tài)注冊(cè)的BroadcastReceiver發(fā)送數(shù)據(jù)
{
Intent intent = new Intent(INTENAL_ACTION_2);
sendBroadcast(intent);
}
else if(v==btnSystem)//動(dòng)態(tài)注冊(cè) 接收2組信息的BroadcastReceiver
{
IntentFilter filter = new IntentFilter();//
filter.addAction(Intent.ACTION_BATTERY_CHANGED);//系統(tǒng)電量檢測(cè)信息
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);//傳遞過去
}
}
}
/*
* 接收動(dòng)態(tài)注冊(cè)廣播的BroadcastReceiver
*/
private BroadcastReceiver bcrIntenal1 = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(context, "動(dòng)態(tài):"+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)) {
//當(dāng)未知Intent包含的內(nèi)容,則需要通過以下方法來列舉
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)) {
//當(dāng)未知Intent包含的內(nèi)容,則需要通過以下方法來列舉
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;
/*
* 接收靜態(tài)注冊(cè)廣播的BroadcastReceiver,
* step1:要到AndroidManifest.xml這里注冊(cè)消息
* <receiver android:name="clsReceiver2">
<intent-filter>
<action
android:name="com.testBroadcastReceiver.Internal_2"/>
</intent-filter>
</receiver>
step2:定義消息的字符串
step3:通過Intent傳遞消息來驅(qū)使BroadcastReceiver觸發(fā)
*/
public class clsReceiver2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(context, "靜態(tài):"+action, 1000).show();
}
}
感興趣的朋友可以調(diào)試運(yùn)行該實(shí)例,希望能夠?qū)Υ蠹业腁ndroid項(xiàng)目開發(fā)起到一點(diǎn)幫助作用。
相關(guān)文章
Android Studio 3.0 gradle提示版本太老
這篇文章主要介紹了Android Studio 3.0 gradle提示版本太老的配置和解決方法。2017-11-11
Android集成騰訊X5實(shí)現(xiàn)文檔瀏覽功能
Android內(nèi)部沒有控件來直接顯示文檔,跳轉(zhuǎn)WPS或其他第三方文檔App體驗(yàn)性不好,使用騰訊X5內(nèi)核能很好的解決的這一問題這篇文章主要介紹了Android集成騰訊X5實(shí)現(xiàn)文檔瀏覽功能,需要的朋友可以參考下2019-10-10
Android Service開發(fā)應(yīng)用實(shí)例
Android的服務(wù)是開發(fā)Android應(yīng)用程序的重要組成部分。不同于活動(dòng)Activity,服務(wù)是在后臺(tái)運(yùn)行,服務(wù)沒有接口,生命周期也與活動(dòng)Activity非常不同。通過使用服務(wù)我們可以實(shí)現(xiàn)一些后臺(tái)操作,比如想從遠(yuǎn)程服務(wù)器加載一個(gè)網(wǎng)頁(yè)等,下面來看看詳細(xì)內(nèi)容,需要的朋友可以參考下2022-12-12
Android Studio3.6中的View Binding初探及用法區(qū)別
這篇文章主要介紹了Android 中的View Binding初探及用法區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android自定義ViewGroup實(shí)現(xiàn)豎向引導(dǎo)界面
這篇文章主要為大家詳細(xì)介紹了Andoird自定義ViewGroup實(shí)現(xiàn)豎向引導(dǎo)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Dcloud的native.js直接撥打電話Android實(shí)例代碼
本文為大家分享了3種利用Dcloud的native.js直接撥打電話實(shí)例代碼,由于iOS系統(tǒng)的限制所以只有Android版實(shí)例2018-09-09
教你如何使用platform密鑰對(duì)apk進(jìn)行簽名
這篇文章主要介紹了教你如何使用platform密鑰對(duì)apk進(jìn)行簽名,需要的朋友可以參考下2014-06-06
Android應(yīng)用中使用Fragment組件的一些問題及解決方案總結(jié)
這里我們講的Fragment主要探討的是support庫(kù)中的Fragment,包括Fragment常遇到的crash崩潰問題,嵌套Fragment收不到onActivityResult()回調(diào)以及一些常用tips等,需要的朋友可以參考下2016-05-05
Android自定義View實(shí)現(xiàn)圓弧進(jìn)度效果逐步完成過程
在Android開發(fā)中,通過自定義View實(shí)現(xiàn)自己想要的效果是作為android開發(fā)程序員的一項(xiàng)必備技能,自定義View對(duì)于android開發(fā)來說也是比較難的一項(xiàng)技術(shù)2023-04-04

