Android:广播优先级、sendOrderedBroadcast、短信电话广播、abortBroadcast、ResultData

使用发送有序广播方法,广播优先级才能生效。

1
2
3
4
5
6
7
8
9
10
11
12
findViewById(R.id.button1).setOnClickListener( new  OnClickListener()
{
                          
     @Override
     public  void  onClick(View v)
     {
         Intent intent =  new  Intent();
         intent.setAction( "com.example.aex56" );
         intent.putExtra( "key" "value" );
         sendOrderedBroadcast(intent,  null ); //发送有序广播
     }
});


XML文件设置广播优先级:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<receiver
     android:name= "com.example.aex56_ordered_broadcast.MyReceiver1"
     android:enabled= "true"
     android:exported= "true"  >
     <intent-filter android:priority= "2500" >
         <action android:name= "com.example.aex56"  />
     </intent-filter>
</receiver>
<receiver
     android:name= "com.example.aex56_ordered_broadcast.MyReceiver2"
     android:enabled= "true"
     android:exported= "true"  >
     <intent-filter android:priority= "3000" >
         <action android:name= "com.example.aex56"  />
     </intent-filter>
</receiver>




接收电话呼出广播,自动添加号码前缀呼出;设置短信接收者最高优先级,拦截短信。

XML权限及优先级设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version= "1.0"  encoding= "utf-8" ?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
     package = "com.example.aex58_ordered_tel"
     android:versionCode= "1"
     android:versionName= "1.0"  >
     <uses-sdk
         android:minSdkVersion= "8"
         android:targetSdkVersion= "16"  />
     <uses-permission android:name= "android.permission.PROCESS_OUTGOING_CALLS"  />
     <uses-permission android:name= "android.permission.RECEIVE_SMS" />
     <application
         android:allowBackup= "true"
         android:icon= "@drawable/ic_launcher"
         android:label= "@string/app_name"
         android:theme= "@style/AppTheme"  >
         <receiver
             android:name= "com.example.aex58_ordered_tel.MyTelReceiver"
             android:enabled= "true"
             android:exported= "true"  >
             <intent-filter>
                 <action android:name= "android.intent.action.NEW_OUTGOING_CALL"  />
             </intent-filter>
         </receiver>
         <receiver
             android:name= "com.example.aex58_ordered_tel.MySmsReceiver"
             android:enabled= "true"
             android:exported= "true"  >
             <intent-filter android:priority= "9999999999999999999999999999999" >
                 <action android:name= "android.provider.Telephony.SMS_RECEIVED" />
             </intent-filter>
         </receiver>
     </application>
</manifest>



电话呼出广播接收:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public  class  MyTelReceiver  extends  BroadcastReceiver
{
     //监测电话号码
     /*1.添加电话呼出权限
      * uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"
      *
      *2.接收系统电话呼出的广播
      * <intent-filter >
      * <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
      * </intent-filter>
      *
      * 其他系统广播:
      * android.intent.action.BOOT_COMPLETED  开机启动广播
      */
           
     @Override
     public  void  onReceive(Context context, Intent intent)
     {
         String telnum = getResultData(); //得到电话号码
         telnum =  "0592"  + telnum;
         setResultData(telnum); //设置拨出的电话号码
     }
}


短信接收者:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public  class  MySmsReceiver  extends  BroadcastReceiver
{
       
     /*
      * 拦截短信
      * 1.添加短信接收权限
      * uses-permission android:name="android.permission.RECEIVE_SMS"
      *
      * 2.设置广播优先级,接收系统收短信的广播
      * <intent-filter android:priority="99999999999999999999999999"> 设置广播优先级
      *     <action android:name="android.provider.Telephony.SMS_RECEIVED"/>接收系统收到短信时的广播
      * </intent-filter>
      *
      * 3.重写onReceive
      * abortBroadcast();
      */
       
       
     @Override
     public  void  onReceive(Context context, Intent intent)
     {
         Log.e( "msg" "mmm" );
         abortBroadcast(); //关闭广播
     }
}




本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1211541,如需转载请自行联系原作者
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值