如何編寫廣播接收器
第一步:需要繼承BroadcastReceiver類,覆寫其中的onReceive()方法.
[java]
class MyBroadcastReceiver extends BroadcastReceiver {
//接收到廣播會被自動調用
@Override
public void onReceive (Context context, Intent intent) {
//從Intent中獲取action
…your code here…
}
}
第二步:定義好廣播接收器還不行,必須向系統注冊以便讓其知道該接收器可以處理哪些廣播事件。
常見方式是采用靜態注冊,修改MENIFEST.xml文件, 在<application></application>中加入receiver標簽.
[html]
<application>
<activity name=""/>
<receiver android:name=".MyBroadcastReceiver">
<!-- intent過濾器,指定可以匹配哪些intent, 一般需要定義action 可以是自定義的也可是系統的 -->
<intent-filter>
<action android:name="com.app.bc.test"/>
</intent-filter>
</receiver>
</application>
第三步:此時我們可以發送一個廣播事件出去,代碼如下:
[java]
Intent intent = new Intent(“com.app.bc.test”);
sendBroadcast(intent);//發送廣播事件
動態注冊廣播接收器
在某個Activity中,我們可以用代碼來實現動態注冊:
[java]
//生成一個BroadcastReceiver對象
SMSReceiver smsReceiver = new SMSReceiver();
//生成一個IntentFilter對象
IntentFilter filter = new IntentFilter();
filter.addAction(“android.provider.Telephony.SMS_RECEIVED”);
//將BroadcastReceiver對象注冊到系統當中
//此處表示該接收器會處理短信事件
TestBC1Activity.this.registerReceiver(smsReceiver, filter);
靜態注冊和動態注冊的區別
1)靜態注冊:在AndroidManifest.xml注冊,android不能自動銷毀廣播接收器,也就是說當應用程序關閉後,還是會接收廣播。
2)動態注冊:在代碼中通過registerReceiver()手工注冊.當程序關閉時,該接收器也會隨之銷毀。當然,也可手工調用unregisterReceiver()進行銷毀。
操作小結
靜態注冊的步驟:
定義廣播接收器,繼承BroadcastReceiver類,覆寫onReceive函數.
在xml文件中注冊監聽器,定義Intent-Filter中感興趣的action操作.
使用sendBroadCast向系統發送對其感興趣的廣播接收器中.
動態注冊的步驟:
[java]
SMSReceiver smsReceiver = new SMSReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(“android.provider.Telephony.SMS_RECEIVED”);
TestBC1Activity.this.registerReceiver(smsReceiver, filter);
(無需在配置文件中注冊接收器)
應用實例
[java]
package com.app.test02;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class BroadCastActivity1 extends Activity{
Intent intent = new Intent();
BroadCastTest1 bCastTest1 = new BroadCastTest1();
// BroadCastTest11 bCastTest11 = new BroadCastTest11();
// BroadCastTest111 bCastTest111 = new BroadCastTest111();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bc1);
//靜態注冊
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent.setAction("bc.test101");
intent.putExtra("name", "靜態的");
sendBroadcast(intent);
// sendOrderedBroadcast(intent, null);
}
});
//動態注冊
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("bc.test102");
BroadCastActivity1.this.registerReceiver(bCastTest1, intentFilter);
intent.setAction("bc.test102");
intent.putExtra("name", "動態的");
sendBroadcast(intent);
// sendOrderedBroadcast(intent, null);
}
});
findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
unregisterReceiver(bCastTest1);
finish();
}
});
}
}
廣播類
[java]
package com.app.test02;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BroadCastTest1 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String name = intent.getStringExtra("name");
Toast.makeText(context, "廣播1:" + name, 1000).show();
System.out.println(1);
}
}
AndroidManifest.xml
[html]
<receiver android:name=".BroadCastTest1">
<intent-filter android:priority="3">
<action android:name="bc.test101"/>
</intent-filter>
</receiver>
布局文件
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="靜態注冊廣播" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="動態注冊廣播" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="退出" />
</LinearLayout>
</LinearLayout>