Android之有序广播


布局代码
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/activity_main"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    android:background="@drawable/stitch_one"  
    tools:context="com.example.bz0209.myapplication.MainActivity">  
  
  
    <Button  
        android:text="发送有序广播"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="58dp"  
        android:id="@+id/button"  
        android:onClick="click"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true" />  
</RelativeLayout>  

代码如下:

public class MyReceiver01 extends BroadcastReceiver {  
    public MyReceiver01() {  
    }  
  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        Log.i("MyReceiver01","自定义广播接收者1,接收到了广播");  
    }  
}  


广播 01 ,02,03与上类同设置优先级代码如下

<receiver  
            android:name=".MyReceiver01">  
            <intent-filter android:priority="100">  
                <action android:name="intercept_Stitch"/>  
            </intent-filter>  
        </receiver>  
  
        <receiver  
            android:name=".MyReceiver02">  
            <intent-filter android:priority="400">  
                <action android:name="intercept_Stitch"/>  
            </intent-filter>  
        </receiver>  
  
  
  
        <receiver  
            android:name=".MyReceiver03">  
            <intent-filter android:priority="600">  
                <action android:name="intercept_Stitch"/>  
            </intent-filter>  
        </receiver>  
    </application>  


修改广播3代码:
public class MyReceiver03 extends BroadcastReceiver {  
    public MyReceiver03() {  
    }  
  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        Log.i("MyReceiver03","自定义广播接收者3,接收到了广播");  
        abortBroadcast();  
        Log.i("MyReceiver03","自定义广播接收者3,终结广播");  
    }  
} 

修改优先级代码

<receiver  
           android:name=".MyReceiver01">  
           <intent-filter android:priority="1000">  
               <action android:name="intercept_Stitch"/>  
           </intent-filter>  
       </receiver>  
  
       <receiver  
           android:name=".MyReceiver02">  
           <intent-filter android:priority="1000">  
               <action android:name="intercept_Stitch"/>  
           </intent-filter>  
       </receiver>  
  
  
  
       <receiver  
           android:name=".MyReceiver03">  
           <intent-filter android:priority="600">  
               <action android:name="intercept_Stitch"/>  
           </intent-filter>  
       </receiver>  
   </application>  
实验结果图:




有序广播,即从优先级别最高的广播接收器开始接收,接收完了如果没有丢弃,就下传给下一个次高优先级别的广播接收器进行处理,依次类推,直到最后。如果多个应用程序设置的优先级别相同,则谁先注册的广播,谁就可以优先接收到广播。这里接收短信的广播是有序广播,因此可以设置你自己的广播接收器的级别高于系统原来的级别,就可以拦截短信,并且不存收件箱,也不会有来信提示音。里面的android:priority="100"就是设定广播接收器的级别,这个值从1000~-1000,数值越大,优先级别就越高。
实现方法是:
<receiverAndroid:name=".SmsReceiver">
   <intent-filterAndroid:priority="100">
    <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>                  
   </intent-filter>
</receiver>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值