有两种注册方式:1. 在AndroidManifest中注册。2. 在代码中直接注册,这种注册需要注意的一点是:当注册此Receiver的Activity退出的时候,一定要调用unregisterReceiver 方法,这个有点像Service的Bind。
BroadcastReceiver的子类别都是无状态的类别,每次收到发送广播事件后,BroadcastReceiver都会创建一个新的对象,然后 再执行onReceive()函数,当onReceive()函数执行完毕后,就立刻删掉该对象,下一次再收到此广播后,又会创建一个新的对象。
step1:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击发送广播"/>
</LinearLayout>
step2:BroadCastActivity.java
package com.lp.ecjtu;
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.view.View.OnClickListener;
import android.widget.Button;
public class BroadCastActivity extends Activity {
/** Called when the activity is first created. */
private Button btn;
private BroadCaster myBroadCast;
class BroadCaster extends BroadcastReceiver{
private static final String TAG = "BroadCaster";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();//接收广播
Log.v(TAG, "this action is"+action);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBroadCast = new BroadCaster();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("test");//携带test行为
this.registerReceiver(myBroadCast, intentFilter);//启动activity时,注册广播
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("test");
BroadCastActivity.this.sendBroadcast(intent);//点击按钮发送广播
}
});
}
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
this.unregisterReceiver(myBroadCast);
}
}
当然我们也可以使用第一种方式,在Android Manifest.xml定义,这样就定义了一个全局的receiver()和Activity <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lp.ecjtu"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BroadCastActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- <receiver android:name=".BroadCastActivity">
<intent-filter >
<action android:name="test"/>
</intent-filter>
</receiver> -->
</application> </manifest>