第一步:需要继承BroadcastReceiver类,覆写其中的onReceive()方法.
class MyBroadcastReceiver extends BroadcastReceiver { //接收到广播会被自动调用 @Override public void onReceive (Context context, Intent intent) { //从Intent中获取action …your code here… } }
第二步:定义好广播接收器还不行,必须向系统注册以便让其知道该接收器可以处理哪些广播事件。
常见方式是采用静态注册,修改MENIFEST.xml文件, 在<application></application>中加入receiver标签.
<application> <activity name=""/> <receiver android:name=".MyBroadcastReceiver"> <!-- intent过滤器,指定可以匹配哪些intent, 一般需要定义action 可以是自定义的也可是系统的 --> <intent-filter> <action android:name="com.app.bc.test"/> </intent-filter> </receiver> </application>
第三步:此时我们可以发送一个广播事件出去,代码如下:
Intent intent = new Intent(“com.app.bc.test”); sendBroadcast(intent);//发送广播事件
//生成一个BroadcastReceiver对象 SMSReceiver smsReceiver = new SMSReceiver(); //生成一个IntentFilter对象 IntentFilter filter = new IntentFilter(); filter.addAction(“android.provider.Telephony.SMS_RECEIVED”); //将BroadcastReceiver对象注册到系统当中 //此处表示该接收器会处理短信事件 TestBC1Activity.this.registerReceiver(smsReceiver, filter);
SMSReceiver smsReceiver = new SMSReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(“android.provider.Telephony.SMS_RECEIVED”); TestBC1Activity.this.registerReceiver(smsReceiver, filter);
(无需在配置文件中注册接收器)
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(); } }); } }
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); } }
<receiver android:name=".BroadCastTest1"> <intent-filter android:priority="3"> <action android:name="bc.test101"/> </intent-filter> </receiver>
<?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>