android的receiver优先级

Android上的一些应用都有拦截短信广播的功能,360,各种手机卫士,还有一些通讯录。最恼人的就是通讯录这些,有的甚至是拦截短信,扔掉广播,由它帮你入库。

经过反编译,有点眉目。360,金山手机卫士的manifest里面根本就没有注册短息的Receiver,所以他们只可能是动态注册短信广播接收器。

还有这个东西:

[html] view plaincopy
  1. <intent-filter android:priority="2147483647">  
优先级他们都会设置成这个很长的int,其实这个数是最大int型整数。


我在网上看到过一些,说是动态注册的广播接收器优先级高于静态注册,此时便很清楚了。

我们可以测试一下,写一个开机启动的Receiver:

[html] view plaincopy
  1. <receiver android:name=".BootReceiver" >  
  2.      <intent-filter android:priority="2147483647" >  
  3.            <action android:name="android.intent.action.BOOT_COMPLETED" />  
  4.      </intent-filter>  
  5. </receiver>  


[html] view plaincopy
  1. <pre>  

在onreceiver里面启动一个service:

[java] view plaincopy
  1. @Override  
  2. public void onReceive(Context context, Intent intent) {  
  3.     Intent intent2 = new Intent();  
  4.     intent2.setClass(context, SmsService.class);  
  5.     context.startService(intent2);  
  6. }  


在这个service里面动态注册短息的广播接收器:

[java] view plaincopy
  1. public class SmsService extends Service {  
  2.     private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";  
  3.   
  4.     @Override  
  5.     public IBinder onBind(Intent intent) {  
  6.         // TODO Auto-generated method stub  
  7.         return null;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void onCreate() {  
  12.         IntentFilter filter = new IntentFilter(ACTION);  
  13.         filter.setPriority(2147483647);  
  14.         MyBrocast myService = new MyBrocast();  
  15.         registerReceiver(myService, filter);  
  16.     }  
  17.   
  18.     private class MyBrocast extends BroadcastReceiver {  
  19.   
  20.         @Override  
  21.         public void onReceive(Context context, Intent intent) {  
  22.             System.out.println("receiver message --->>>>");  
  23.             abortBroadcast();  
  24.         }  
  25.   
  26.     }  
  27.   
  28. }  
运行程序,然后重启,给模拟器发送短信,结果是测试程序给拦掉了,金山,360没有反映。


说下这个有序广播的优先级问题。以下有部分我没有测试过,也是四处看的,如果有错,请您纠正。

动态注册优先级别高于静态注册

在动态注册中
最早动态注册优先级别最高

在静态注册中
最早安装的程序,静态注册优先级别最高(安装APK会解析manifest.xml,把其加入队列)
这里安装的应用不包括rom里面的应用。
然后才是adb push到其他目录的应用。
可能的原因是手机查询应用的时候会先去特定目录解析应用,所以广播注册会出现这种差别。adb push 到system/app下会比安装的优先级高吗?这有待验证,我还没测试过。
然后都是安装的应用中,首先安装的优先等级最高。

个人认为这种有序广播存在很大的不足,导致大量第三方应用程序滥用方法去抢夺广播优先级。

暂时就这么多了……

发布了18 篇原创文章 · 获赞 5 · 访问量 19万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章