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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章