BroadcastReceive广播接收器:
public class Test extends Activity{
private final String ACTION_NAME = "发送广播";
private Button mBtnMsgEvent = null;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//注册广播
registerBoradcastReceiver();
LinearLayout mLinearLayout = new LinearLayout(this);
mBtnMsgEvent = new Button(this);
mBtnMsgEvent.setText("发送广播");
mLinearLayout.addView(mBtnMsgEvent);
setContentView(mLinearLayout);
mBtnMsgEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(ACTION_NAME);
mIntent.putExtra("yaner", "发送广播,相当于在这里传送数据");
//发送广播
sendBroadcast(mIntent);
}
});
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(ACTION_NAME)){
Toast.makeText(Test.this, "处理action名字相对应的广播", 200);
}
}
};
public void registerBoradcastReceiver(){
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
//注册广播
registerReceiver(mBroadcastReceiver, myIntentFilter);
}
}
//切记关闭广播
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(receiver);
}
<span style="font-size:18px;">你的应用可以使用它对外部事件进行过滤只对感兴趣的外部事件(如当电话呼入时,或者数据网络可用时)进行接收并做出响应。广播接收器没有用户界面。然而,它们可以启动一个activity或serice 来响应它们收到的信息,或者用NotificationManager 来通知用户。通知可以用很多种方式来吸引用户的注意力──闪动背灯、震动、播放声音等。一般来说是在状态栏上放一个持久的图标,用户可以打开它并获取消息。
广播类型:
普通广播,通过Context.sendBroadcast(Intent myIntent)发送的</span>
<span style="font-size:18px;">
有序广播,通过Context.sendOrderedBroadcast(intent, receiverPermission)发送的,该方法第2个参数决定该广播的级别,级别数值是在 -1000 到 1000 之间 , 值越大 , 发送的优先级越高;广播接收者接收广播时的级别级别(可通过intentfilter中的priority进行设置设为2147483647时优先级最高),同级别接收的先后是随机的, 再到级别低的收到广播,高级别的或同级别先接收到广播的可以通过abortBroadcast()方法截断广播使其他的接收者无法收到该广播,还有其他构造函数</span>
<span style="font-size:18px;">
异步广播,通过Context.sendStickyBroadcast(Intent myIntent)发送的,还有sendStickyOrderedBroadcast(intent, resultReceiver, scheduler, initialCode, initialData, initialExtras)方法,该方法具有有序广播的特性也有异步广播的特性;发送异步广播要: <uses-permission android:name="android.permission.BROADCAST_STICKY" />权限,接收并处理完Intent后,广播依然存在,直到你调用removeStickyBroadcast(intent)主动把它去掉</span>
<span style="font-size:18px;">
注意:发送广播时的intent参数与Contex.startActivity()启动起来的Intent不同,前者可以被多个订阅它的广播接收器调用,后者只能被一个(Activity或service)调用
监听广播Intent步骤:
1> </span>
<span style="font-size:18px;"> 写一个继承BroadCastReceiver的类,重写onReceive()方法,广播接收器仅在它执行这个方法时处于活跃状态。当onReceive()返回后,它即为失活状态,注意:为了保证用户交互过程的流畅,一些费时的操作要放到线程里,如类名SMSBroadcastReceiver</span>
<span style="font-size:18px;">
2> </span>
<span style="font-size:18px;"> 注册该广播接收者,注册有两种方法程序动态注册和AndroidManifest文件中进行静态注册(可理解为系统中注册)如下:</span>
<span style="font-size:18px;">
静态注册,注册的广播,下面的priority表示接收广播的级别"2147483647"为最高优先级
<receiver android:name=".SMSBroadcastReceiver"></span>
<span style="font-size:18px;"><intent-filter android:priority = "2147483647"></span>
<span style="font-size:18px;"><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver ></span>
<span style="font-size:18px;">
动态注册,一般在Activity可交互时onResume()内注册BroadcastReceiver
IntentFilter intentFilter=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mBatteryInfoReceiver ,intentFilter);
//反注册
unregisterReceiver(receiver);
注意:
1.生命周期只有十秒左右,如果在 onReceive() 内做超过十秒内的事情,就会报ANR(Application No Response) 程序无响应的错误信息,如果需要完成一项比较耗时的工作 , 应该通过发送 Intent 给 Service, 由Service 来完成 . 这里不能使用子线程来解决 , 因为 BroadcastReceiver 的生命周期很短 , 子线程可能还没有结束BroadcastReceiver 就先结束了 .BroadcastReceiver 一旦结束 , 此时 BroadcastReceiver 的所在进程很容易在系统需要内存时被优先杀死 , 因为它属于空进程 ( 没有任何活动组件的进程 ). 如果它的宿主进程被杀死 , 那么正在工作的子线程也会被杀死 . 所以采用子线程来解决是不可靠的</span>
<span style="font-size:18px;">
2. 动态注册广播接收器还有一个特点,就是当用来注册的Activity关掉后,广播也就失效了。静态注册无需担忧广播接收器是否被关闭,只要设备是开启状态,广播接收器也是打开着的。也就是说哪怕app本身未启动,该app订阅的广播在触发时也会对它起作用
系统常见广播Intent,如开机启动、电池电量变化、时间改变等广播</span>