BroadCastReceiver 简介
广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadcast() 、 Context.sendOrderedBroadcast() 来实现的。通常一个广播 Intent 可以被订阅了此 Intent 的多个广播接收者所接收。
广播是一种广泛运用的在应用程序之间传输信息的机制 。而 BroadcastReceiver 是对发送出来的广播进行过滤接收并响应的一类组件;
来自普通应用程序,如一个应用程序通知其他应用程序某些数据已经下载完毕。
BroadcastReceiver 自身并不实现图形用户界面,但是当它收到某个通知后, BroadcastReceiver 可以启动 Activity 作为响应,或者通过 NotificationMananger 提醒用户,或者启动 Service 等等。
生命周期
一个BroadcastReceiver 对象只有在被调用onReceive(Context, Intent)的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。
因此从这个特征可以看出,在所调用的onReceive(Context, Intent)函数里,不能有过于耗时的操作,不能使用线程来执行。对于耗时的操作,请start service来完成。因为当得到其他异步操作所返回的结果时,BroadcastReceiver 可能已经无效了。
使用BroadcastReceiver
编写类继承BroadcastReceiver,复写onReceiver()方法
package com.example.receive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceivceReg extends BroadcastReceiver {
private static final String TAG = "MyReceivce";
public MyReceivceReg(){
Log.i(TAG,"MyReceivceReg"); }
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"onReceiveReg");
}
}
在Manifest.xml中注册BroadcastReceiver
<receiver android:name="com.example.receive.MyReceivce">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
要接收某些action,需要在AndroidManifest.xml里面添加相应的permission。例如接收SMS:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
……
</manifest>
界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AndroidBroadcastServiceActivity" > <Button
android:id="@+id/btnStartBroad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动Broadservice"
>
</Button> <Button
android:id="@+id/btnRegisterBroad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册RegisterBroad"
>
</Button> <Button
android:id="@+id/btnUnRegisterBroad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消RegisterBroad"
>
</Button> </LinearLayout>
构建Intent,发送
package com.example.androidbroadcastservice; import com.example.receive.MyReceivceReg; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class AndroidBroadcastServiceActivity extends Activity {
protected static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";//广播类型。其实是对应Manifest.xml中<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
private Button btnStartBroad,btnRegisterBroad,btnUnRegisterBroad;
private MyReceivceReg receivece;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_broadcast_service);
btnStartBroad = (Button) this.findViewById(R.id.btnStartBroad);
btnStartBroad.setOnClickListener(onclick); btnRegisterBroad = (Button) this.findViewById(R.id.btnRegisterBroad);
btnRegisterBroad.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
receivece=new MyReceivceReg();
IntentFilter filter=new IntentFilter();
filter.addAction(ACTION);
registerReceiver(receivece, filter);
}
}); btnUnRegisterBroad = (Button) this.findViewById(R.id.btnUnRegisterBroad);
btnUnRegisterBroad.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//MyReceivceReg receivece=new MyReceivceReg();
//IntentFilter filter=new IntentFilter(ACTION);
unregisterReceiver(receivece);
}
});
}
private OnClickListener onclick = new OnClickListener() { @Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction(ACTION);
sendBroadcast(intent);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_android_broadcast_service,
menu);
return true;
} }