Android開(kāi)發(fā)應(yīng)用中Broadcast Receiver組件詳解
BroadcastReceiver(廣播接收器)是Android中的四大組件之一。下面就具體介紹一下Broadcast Receiver組件的用法。
下面是Android Doc中關(guān)于BroadcastReceiver的概述:
①?gòu)V播接收器是一個(gè)專(zhuān)注于接收廣播通知信息,并做出對(duì)應(yīng)處理的組件。很多廣播是源自于系統(tǒng)代碼的──比如,通知時(shí)區(qū)改變、電池電量低、拍攝了一張照片或者用戶(hù)改變了語(yǔ)言選項(xiàng)。應(yīng)用程序也可以進(jìn)行廣播──比如說(shuō),通知其它應(yīng)用程序一些數(shù)據(jù)下載完成并處于可用狀態(tài)。
②應(yīng)用程序可以擁有任意數(shù)量的廣播接收器以對(duì)所有它感興趣的通知信息予以響應(yīng)。所有的接收器均繼承自BroadcastReceiver基類(lèi)。
③廣播接收器沒(méi)有用戶(hù)界面。然而,它們可以啟動(dòng)一個(gè)activity來(lái)響應(yīng)它們收到的信息,或者用NotificationManager來(lái)通知用戶(hù)。通知可以用很多種方式來(lái)吸引用戶(hù)的注意力──閃動(dòng)背燈、震動(dòng)、播放聲音等等。一般來(lái)說(shuō)是在狀態(tài)欄上放一個(gè)持久的圖標(biāo),用戶(hù)可以打開(kāi)它并獲取消息。
Android中的廣播事件有兩種,一種就是系統(tǒng)廣播事件,比如:ACTION_BOOT_COMPLETED(系統(tǒng)啟動(dòng)完成后觸發(fā)),ACTION_TIME_CHANGED(系統(tǒng)時(shí)間改變時(shí)觸發(fā)),ACTION_BATTERY_LOW(電量低時(shí)觸發(fā))等等。另外一種是我們自定義的廣播事件。
廣播事件的流程
①注冊(cè)廣播事件:注冊(cè)方式有兩種,一種是靜態(tài)注冊(cè),就是在AndroidManifest.xml文件中定義,注冊(cè)的廣播接收器必須要繼承BroadcastReceiver;另一種是動(dòng)態(tài)注冊(cè),是在程序中使用Context.registerReceiver注冊(cè),注冊(cè)的廣播接收器相當(dāng)于一個(gè)匿名類(lèi)。兩種方式都需要IntentFIlter。
②發(fā)送廣播事件:通過(guò)Context.sendBroadcast來(lái)發(fā)送,由Intent來(lái)傳遞注冊(cè)時(shí)用到的Action。
③接收廣播事件:當(dāng)發(fā)送的廣播被接收器監(jiān)聽(tīng)到后,會(huì)調(diào)用它的onReceive()方法,并將包含消息的Intent對(duì)象傳給它。onReceive中代碼的執(zhí)行時(shí)間不要超過(guò)5s,否則Android會(huì)彈出超時(shí)dialog。
下面我通過(guò)代碼演示自定義廣播事件和系統(tǒng)廣播事件的使用。
Step1:在MainActivity的onStart方法中注冊(cè)廣播事件。靜態(tài)注冊(cè)方式是在AndroidManifest.xml文件中。
Step2: 點(diǎn)擊相應(yīng)按鈕后會(huì)觸發(fā)相應(yīng)的方式來(lái)發(fā)送廣播消息。
/**
* MainActivity
* @author zuolongsnail
*
*/
public class MainActivity extends Activity {
private Button sendStaticBtn;
private Button sendDynamicBtn;
private Button sendSystemBtn;
private static final String STATICACTION = "com.byread.static";
private static final String DYNAMICACTION = "com.byread.dynamic";
// USB設(shè)備連接
private static final String SYSTEMACTION = Intent.ACTION_POWER_CONNECTED;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendStaticBtn = (Button) findViewById(R.id.send_static);
sendDynamicBtn = (Button) findViewById(R.id.send_dynamic);
sendSystemBtn = (Button) findViewById(R.id.send_system);
sendStaticBtn.setOnClickListener(new MyOnClickListener());
sendDynamicBtn.setOnClickListener(new MyOnClickListener());
sendSystemBtn.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// 發(fā)送自定義靜態(tài)注冊(cè)廣播消息
if(v.getId() == R.id.send_static){
Log.e("MainActivity", "發(fā)送自定義靜態(tài)注冊(cè)廣播消息");
Intent intent = new Intent();
intent.setAction(STATICACTION);
intent.putExtra("msg", "接收靜態(tài)注冊(cè)廣播成功!");
sendBroadcast(intent);
}
// 發(fā)送自定義動(dòng)態(tài)注冊(cè)廣播消息
else if(v.getId() == R.id.send_dynamic){
Log.e("MainActivity", "發(fā)送自定義動(dòng)態(tài)注冊(cè)廣播消息");
Intent intent = new Intent();
intent.setAction(DYNAMICACTION);
intent.putExtra("msg", "接收動(dòng)態(tài)注冊(cè)廣播成功!");
sendBroadcast(intent);
}
// 發(fā)送系統(tǒng)動(dòng)態(tài)注冊(cè)廣播消息。當(dāng)手機(jī)連接充電設(shè)備時(shí)會(huì)由系統(tǒng)自己發(fā)送廣播消息。
else if(v.getId() == R.id.send_system){
Log.e("MainActivity", "發(fā)送系統(tǒng)動(dòng)態(tài)注冊(cè)廣播消息");
Intent intent = new Intent();
intent.setAction(SYSTEMACTION);
intent.putExtra("msg", "正在充電。。。。");
}
}
}
@Override
protected void onStart() {
super.onStart();
Log.e("MainActivity", "注冊(cè)廣播事件");
// 注冊(cè)自定義動(dòng)態(tài)廣播消息
IntentFilter filter_dynamic = new IntentFilter();
filter_dynamic.addAction(DYNAMICACTION);
registerReceiver(dynamicReceiver, filter_dynamic);
// 注冊(cè)系統(tǒng)動(dòng)態(tài)廣播消息
IntentFilter filter_system = new IntentFilter();
filter_system.addAction(SYSTEMACTION);
registerReceiver(systemReceiver, filter_system);
}
private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("MainActivity", "接收自定義動(dòng)態(tài)注冊(cè)廣播消息");
if(intent.getAction().equals(DYNAMICACTION)){
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
};
private BroadcastReceiver systemReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("MainActivity", "接收系統(tǒng)動(dòng)態(tài)注冊(cè)廣播消息");
if(intent.getAction().equals(SYSTEMACTION)){
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
};
}
Step3:接收廣播消息。以下為兩個(gè)靜態(tài)注冊(cè)的廣播接收器。
/**
* 自定義靜態(tài)注冊(cè)廣播消息接收器
* @author zuolongsnail
*
*/
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
/**
* 系統(tǒng)靜態(tài)注冊(cè)廣播消息接收器
*
* @author zuolongsnail
*
*/
public class SystemReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {
Log.e("SystemReceiver", "電量低提示");
Toast.makeText(context, "您的手機(jī)電量偏低,請(qǐng)及時(shí)充電", Toast.LENGTH_SHORT).show();
}
}
}
下面是AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byread" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注冊(cè)自定義靜態(tài)廣播接收器 -->
<receiver android:name=".StaticReceiver">
<intent-filter>
<action android:name="com.byread.static" />
</intent-filter>
</receiver>
<!-- 注冊(cè)系統(tǒng)靜態(tài)廣播接收器 -->
<receiver android:name=".SystemReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>
</application>
</manifest>
界面布局文件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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/send_static" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="發(fā)送自定義靜態(tài)注冊(cè)廣播" />
<Button android:id="@+id/send_dynamic" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="發(fā)送自定義動(dòng)態(tài)注冊(cè)廣播" />
<Button android:id="@+id/send_system" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="發(fā)送系統(tǒng)動(dòng)態(tài)注冊(cè)廣播" />
</LinearLayout>
講解結(jié)束,不過(guò)有一點(diǎn)我自己也沒(méi)弄清楚,這個(gè)系統(tǒng)廣播事件如果我在程序中sendBroadcast的話(huà),那就是自定義廣播了。如果不寫(xiě)的話(huà),那是不是系統(tǒng)自己來(lái)發(fā)送對(duì)應(yīng)Action廣播呢?有知道的同學(xué)請(qǐng)告訴我一下,再此先謝過(guò)。
運(yùn)行界面:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Activity 完全結(jié)束并退出程序的實(shí)例
2013-11-11
Android實(shí)現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例
本篇文章主要介紹了Android實(shí)現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02
Android開(kāi)發(fā)基于ViewPager+GridView實(shí)現(xiàn)仿大眾點(diǎn)評(píng)橫向滑動(dòng)功能
這篇文章主要介紹了Android開(kāi)發(fā)基于ViewPager+GridView實(shí)現(xiàn)仿大眾點(diǎn)評(píng)橫向滑動(dòng)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android提高之BLE開(kāi)發(fā)Android手機(jī)搜索iBeacon基站
這篇文章主要介紹了BLE開(kāi)發(fā)Android手機(jī)搜索iBeacon基站,需要的朋友可以參考下2014-08-08
Android仿微博個(gè)人詳情頁(yè)滾動(dòng)到頂部的實(shí)例代碼
這篇文章主要介紹了Android仿微博個(gè)人詳情頁(yè)滾動(dòng)到頂部的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒家,需要的朋友可以參考下2019-05-05
浮動(dòng)AppBar中的textField焦點(diǎn)回滾問(wèn)題解決
這篇文章主要為大家介紹了浮動(dòng)AppBar中的textField焦點(diǎn)回滾問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
用Android Studio3.0新功能加快構(gòu)建速度
本文主要介紹了使用Android Studio3.0新功能,加快Android Studio的構(gòu)建速度等相關(guān)做法。2017-11-11
Android底部導(dǎo)航欄的三種風(fēng)格實(shí)現(xiàn)
這篇文章主要介紹了Android底部導(dǎo)航欄的三種風(fēng)格實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Android Studio實(shí)現(xiàn)帶邊框的圓形頭像
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)帶邊框的圓形頭像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Retrofit 創(chuàng)建網(wǎng)絡(luò)請(qǐng)求接口實(shí)例過(guò)程
這篇文章主要為大家介紹了Retrofit 創(chuàng)建網(wǎng)絡(luò)請(qǐng)求接口實(shí)例過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

