android BroadcastReceiver广播

為{幸葍}努か 提交于 2020-04-11 12:11:36

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文件中静态注册:         

            <receiver android:name="******.MyBroadcastReceiver" 

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

                <intent-filter>

                    <action android:name="android.intent.action.BOOT_COMPLETED" />

                </intent-filter>

            </receiver>

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

        2、代码中动态注册:        

               MyBroadcastReceiver     myBroadcastReceiver = 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);

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






        

        



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!