05
2017
10

安卓开发-Broadcast接受者+六种常见Broadcast接受者案例+进程的优先级

《一:广播接受者》

1.自定义广播

1.1:自定义一个类ReceiverBroadcast,并继承BroadcastReceiver:
//接收方
    public class ReveiverBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v("aaa", "接收到广播了");
        }

    }
1.2:在AndroidManifest.xml中配置:
<receiver android:name="com.m520it.receiverbroadcast.ReveiverBroadcast">
  <intent-filter >
     <action android:name="com.m520it.receiverbroadcast.action.receiver"/>
  </intent-filter>
 </receiver>
1.3:隐式指定action(隐式意图用在跨应用中,因为无法拿到其他应用的组件名)【发送和接收在同一个应用中时可用显示意图】,并发送广播:
//发送方
Intent intent = new Intent("com.m520it.receiverbroadcast.action.receiver");
sendBroadcast(intent);


2.有序广播和无序广播:

2.1:在接收方应用中,定义三个接收广播,并配置广播:
public class ReceiverABroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v("aaa", getResultData() + "A");
            //特点1:有序广播可以在广播途中被中间接受者修改数据
            setResultData("不行");
        }
    }


    public class ReceiverBBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v("aaa", getResultData() + "B");

            //特点2:有序广播可在中间接受者中关闭广播
            abortBroadcast();
        }

    }




    public class ReceiverCBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v("aaa", getResultData() + "C");
        }

    }





    <!-- 此处的action都是相同的,方便同一广播被很多应用监听到 -->
<receiver android:name="com.m520it.receiverbroadcast.ReceiverABroadcast">
    <!-- 配置广播的监听优先级 -->
           <intent-filter android:priority="1000">
    <!-- 配置广播的action,以便通过隐式启动广播被此广播接收到 -->
               <action android:name="com.m520it.receiverbroadcast.action.SEND"/>
           </intent-filter>
 </receiver>
 <receiver android:name="com.m520it.receiverbroadcast.ReceiverBBroadcast">
           <intent-filter android:priority="500">
               <action android:name="com.m520it.receiverbroadcast.action.SEND"/>
           </intent-filter>
 </receiver>
 <receiver android:name="com.m520it.receiverbroadcast.ReceiverCBroadcast">
          <intent-filter android:priority="250">
              <action android:name="com.m520it.receiverbroadcast.action.SEND"/>
          </intent-filter>
</receiver>
2.2:在发送方:
Intent intent = new Intent("com.m520it.receiverbroadcast.action.SEND");

//1.receiverPermission:设置查看广播的权限 
//2.resultReceiver:结果广播接受者,它无需配置 
//3.scheduler:Handler类型,一般不用 
//4.initialCode:初始化编码,来区别广播 
//5.initialData:广播名 
//6.initialExtras:Bundle传递的各种数据 
sendOrderedBroadcast(intent,
             null,
             new Receiver(),
             null,
             0,
             "可以",
             null);

//无需配置
public class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("520it", getResultData() + "Reciver");
    }

}

2.3.1:有序广播相比于无需广播,特点
        特点1:有序广播可以在广播途中被中间接受者修改数据
        特点2:有序广播可在中间接受者中关闭广播
        特点3:配置广播的监听优先级:


2.3.2:无需广播接收者不强调顺序,宏观上认为同时接收到。

注意:

1.不管是否关闭广播,结果广播接受者都能接收到最后一次数据。 2.若广播发送方和接收方在一个应用中,则接收方运行在主线程中,此处应该新开启一个子线程进行耗时操作(网络操作)。

《二:六种常见的广播接受者实例》

一:拨打电话接受者-有序广播(优先级在-1000以下)

1.创建一个类PhoneReceiver 继承 BroadcastReceiver:

    public class PhoneReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v("aaa", "onReceiver");
        }

    }
2.在Manifest.xml中配置:
<receiver android:name="com.m520it.phonereceiver.PhoneReceiver">
     <intent-filter >
         <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
     </intent-filter>
 </receiver>

  <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />(application外,监听拨打是敏感操作)

二:SD卡安装和卸载接受者-无序广播

1.创建一个类SDCardStatusReceiver 继承 BroadcastReceiver:
public class SDCardStatusReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
                Log.v("aaa", "MOUNTED");
            }else if(intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)){
                Log.v("aaa", "UNMOUNTED");
            }
        }

    }
2.在Manifest.xml中配置:
 <receiver android:name="com.m520it.sdcardstatusreceiver.SDCardStatusReceiver">
            <intent-filter >
                <action android:name="android.intent.action.MEDIA_MOUNTED"/>
                <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>(sd卡就是文件)
                <data android:scheme="file"/>
            </intent-filter>
        </receiver>

(只是读取SD卡的安装状态并没有读写,不敏感)

三:短信接受者:本应用优先收到短信,其他应用不能收到短信-有序广播

1.创建一个类SmsReceiver 继承 BroadcastReceiver:
@Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        //短信本身就是报文,以字节流的形式发送,发送时可分多组
        Object[] smsObject = (Object[]) extras.get("pdus");

        String content = "";
        String phone = "";
        for (int i = 0; i < smsObject.length; i++) {
            //通过每个组的信息字节数组得到该段信息对象
            SmsMessage message = SmsMessage.createFromPdu((byte[])smsObject[i]);

            if(i == 0){
                //得到电话号码
                phone = message.getDisplayOriginatingAddress();
            }
            //得到信息内容
            content += message.getDisplayMessageBody();
        }
        Log.v("aaa", phone + "," + content);
        //该应用接收到短信后就关闭广播,截断向其他应用发送信息的可能
        abortBroadcast();
    }


注意:此处android.telephony.gsm.SmsMessage已经过时,应导入android.telephony.SmsMessage

2.在Manifest.xml中配置:
<receiver android:name="com.m520it.smsreceiver.SmsReceiver">
        <!-- 接收短信的优先级,整型数据:-1000~1000 -->
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>

    <!-- 接收短信权限 -->
    <uses-permission android:name="android.permission.RECEIVE_SMS" />

四:应用安装和卸载状态接受者-无序广播

1.创建一个类ApkStatusReceiver 继承 BroadcastReceiver:
@Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)){
            Log.v("aaa", "ACTION_PACKAGE_ADDED");
        }else if(intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)){
            Log.v("aaa", "ACTION_PACKAGE_REMOVED");
        }

    }
2.在Manifest.xml中配置:

<receiver android:name="com.m520it.apkstatusreceiver.ApkStatusReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED"/>
            <action android:name="ansdroid.intent.action.PACKAGE_REMOVED"/>
        //官方规定添加
            <data android:scheme="package"/>
        </intent-filter>
 </receiver>

五:监听开机的广播接受者-无序广播

    可在配置了以下任意一个动作的receiver广播中开启电话监听服务
    //安装SD卡动作
    <action android:name="android.intent.action.MEDIA_MOUNTED" />
    //开机动作
    <action android:name="android.intent.action.BOOT_COMPLETED" />

    //开机的权限
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

六:锁屏与解锁的广播接受者-无序广播

1.创建监听服务类并在AndroidManifest.xml中配置该服务:
public class ScreenBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                Log.v("aaa", "锁屏了");
            }else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
                Log.v("aaa", "解锁啦");
            }
        }

    }

2.在MainActivity中组册广播动作:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //锁屏与解屏适合频繁的操作,不适合在配置文件中配置,在代码中注册:
        mScreen = new ScreenBroadcast();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);

        registerReceiver(mScreen, filter);
    }

    //关闭界面时应销毁注册的动作
    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(mScreen);
    }

《三:应用进程的优先级》

1.Foreground progress(前台进程):
当一个应用的某个页面处在屏幕的最前面,可认为是前台进程(界面最前)

2.Visible progress(可见进程):
如果一个应用启动另一个应用的界面(透明),前者处于可见进程(界面可见,但不是最前了)

3.Service progress(服务进程):
进程里面有一个服务处于运行状态

4.Background progress(后台进程):
如果一个应用没有服务正在运行,并且界面没有关闭,最小化了

5.Empty progress(空进程):
应用程序没有任何活动的组件(无界面,无活动)

上一篇:LruCache 源码解析 下一篇:android 跳转当前app权限页面