當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > 廣播接收器的常用方法
廣播接收器是Android四大組件之一, 是Android組件之間的通信一種方式,適用于以下情況:
第一種 : 同一app內(nèi)部的同一組件內(nèi)的消息通信(單個(gè)或多個(gè)線程之間);
第二種 : 同一app內(nèi)部的不同組件之間的消息通信(單個(gè)進(jìn)程);
第三種 : 同一app具有多個(gè)進(jìn)程的不同組件之間的消息通信;
第四種 : 不同app之間的組件之間消息通信;
第五種 : Android系統(tǒng)在特定情況下與App之間的消息通信。
廣播接收器的生命周期
只有一個(gè) onReceive()方法
發(fā)送廣播
第一種 :顯示廣播 Intent intent = new Intent(this, MyReceicer.class); 傳遞包含信息
第二種 :隱式廣播 應(yīng)用 action --- "暗號(hào)"
Intent intent = new Intent(action);
在清單文件中 注冊(cè)廣播
BroadcastReceiver總體上可以分為兩種注冊(cè)類型:靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)
例子 1:普通廣播: 自定義一個(gè)廣播接收器類,實(shí)現(xiàn)能接收廣播信息
按鈕觸發(fā)-----> 發(fā)送廣播 -----> 自定義廣播接收器 ----> 吐司顯示
(1)創(chuàng)建自定義廣播接收器類 MyBroadcastReceiver 繼承 BroadcastReceiver
覆蓋 onReceive() 方法 寫入執(zhí)行的操作(不能太耗時(shí)):
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, intent.getStringExtra("name"),
Toast.LENGTH_LONG).show();
}
}
自定義廣播接收器需要繼承基類BroadcastReceivre,并實(shí)現(xiàn)抽象方法onReceive(context, intent)方法。廣播接收器接收到相應(yīng)廣播后,會(huì)自動(dòng)回到onReceive(..)方法。
(2)靜態(tài)注冊(cè)廣播接收器 : 在AndroidManifest.xml 清單文件中加入 receiver聲明
........................
(3)在活動(dòng)的布局文件中,設(shè)置 一個(gè)按鈕 Button 設(shè)置id 長(zhǎng) 寬
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="46dp"
android:layout_marginTop="71dp"
android:layout_toRightOf="@+id/textView1"
android:onClick="onClick" 設(shè)置監(jiān)聽
android:text="點(diǎn)擊發(fā)送廣播" />
(4)在布局文件對(duì)應(yīng)的.java活動(dòng)文件中, 出現(xiàn)
鼠標(biāo)點(diǎn)擊 選擇第一項(xiàng)添加onClick()方法
在方法內(nèi)添加 發(fā)送廣播命令語(yǔ)句:這是只要 用戶點(diǎn)擊按鈕,就會(huì)發(fā)送廣播
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent("mybroadcast");
in.putExtra("name", "zhangsan");
sendBroadcast(in);
}
(5)啟動(dòng)模擬器, 運(yùn)行當(dāng)前的android 工程, 點(diǎn)擊按鈕,觀察是否有吐司出現(xiàn)。
例子2:使用系統(tǒng)廣播,獲取電池電量,并用動(dòng)態(tài)注冊(cè)廣播接收器方式實(shí)現(xiàn):
(1)在布局文件中, 加入一個(gè)進(jìn)度條:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
style="?android:attr/progressBarStyleHorizontal"
android:layout_gravity="bottom">
(2)在主Activity中, 聲明一個(gè)屬性 ProgressBar pb;
(3)在onCreate() 中 pb 綁定id :
pb = (ProgressBar) findViewById(R.id.progressBar1);
(4)自定義一個(gè)類BatteryReceiver 繼承 BroadcastReceiver
覆蓋 onReceive() 方法:
private class BatteryReceiver extends BroadcastReceiver {
int level = 0;
int scale = -1;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (scale == -1) {
scale = intent.getIntExtra("scale", 0);
pb.setMax(scale);
}
level = intent.getIntExtra("level", 0);
pb.setProgress(level);
}
}
(5)在onCreate()方法中, 動(dòng)態(tài)注冊(cè)廣播接收器:
動(dòng)態(tài)注冊(cè)時(shí),無須在AndroidManifest中注冊(cè)組件。直接在代碼中通過調(diào)用Context的registerReceiver函數(shù),可以在程序中動(dòng)態(tài)注冊(cè)BroadcastReceiver。
添加:
registerReceiver(new BatteryReceiver(), new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
后onCreate()方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb = (ProgressBar) findViewById(R.id.progressBar1);
registerReceiver(new BatteryReceiver(), new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
(6)打開模擬器, 執(zhí)行Android 程序。