android 动态广播的生命周期,android BroadcastReceiver广播

Broadcast是一种广泛运用的在应用程序之间传输信息的机制。

一、下面是几个常用的系统广播

//重启设备时的广播

Intent.ACTION_REBOOT;

//屏幕被关闭之后的广播

Intent.ACTION_SCREEN_OFF;

//屏幕被打开之后的广播

Intent.ACTION_SCREEN_ON;

//在系统启动完成后,这个动作被广播一次(只有一次)。

Intent.ACTION_BOOT_COMPLETED;

//识别用户进入home界面

Intent.ACTION_USER_PRESENT;

二、BroadcastReceiver总体上可以分为两种注册类型:静态注册和动态注册:

1、AndroidManifest.xml文件中静态注册:

android:exported=["true" | "false"]   >

android:exported:此broadcastReceiver能否接收其他App的发出的广播,这个属性默认是由receiver中有                                     无intent-filter决定的,如果有intent-filter,默认值为true,否则为false。

2、代码中动态注册:

MyBroadcastReceivermyBroadcastReceiver = new MyBroadcastReceiver();

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(android.intent.action.BOOT_COMPLETED);

registerReceiver(myBroadcastReceiver, intentFilter);

三、广播接收的生命周期(10s):

在Android中,每次广播消息到来时都会创建BroadcastReceiver实例并执行onReceive() 方法,onReceive() 方法执行完后,BroadcastReceiver 的实例就会被销毁,当onReceive() 方法在10秒内没有执行完毕,Android会认为该程序无响应,会弹出 ANR的对话框。所以在onReceive() 里不能做一些比较耗时的操作,如果需要完成一项比较耗时的工作,应该通过发送Intent给Service,由Service来完成。

四、程序被强制停止无法接收广播:

官方说明:

Launch controls on stopped applications

Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.

Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.

FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against.

FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets.

When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).

依据上面的说明自定义广播只需要在发送广播时设置一个标识就可以解决在程序被强制停止时仍然可以接收广播:

Intent intent = new Intent();

intent.setAction("******");

if (android.os.Build.VERSION.SDK_INT >= 12) {

intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);//3.1以后的版本需要设置

}

sendBroadcast(intent);

而对于系统广播,目前还没有找到解决办法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值