Android開發22——廣播接收者BroadcastReceiver的原理和註冊

本文出自 “IT徐胖子的專欄” 博客,請務必保留此出處http://woshixy.blog.51cto.com/5637578/1097197


一、廣播機制的基本概念

當某個事件產生時(如一條短信發來或一個電話打來),android操作系統會把這個事件廣播給所有註冊的廣播接收者,需要處理這個事件的廣播接收者進行處理。其實這就是日常生活中的廣播。發生一個新聞後,廣播電臺會廣播這個新聞給打開收音機的人,對這個新聞感興趣的人會關注,可能會拿筆記下。新聞就是事件,廣播電臺就是android系統,打開收音機的人就是廣播接收者,感興趣的人就是需要處理該事件的廣播接收者,拿筆記下就是對該事件進行的操作。



二、廣播的分類——普通廣播和有序廣播

①普通廣播:完全異步,邏輯上可以被任何廣播接收者接收到。優點是效率較高。缺點是一個接收者不能將處理結果傳遞給下一個接收者,並無法終止廣播intent的傳播。


②有序廣播:按照被接收者的優先級順序,在被接收者中一次傳播。比如有三個廣播接收者A,B,C,優先級是A > B > C。那這個消息先傳給A,再傳給B,最後傳給C。每個接收者有權中終止廣播,比如B終止廣播,C就無法接收到。此外A接收到廣播後可以對結果對象進行操作,當廣播傳給B時,B可以從結果對象中取得A存入的數據。如系統收到短信發出的廣播就是有序廣播。



三、註冊廣播接收者的兩種方式

①在AndroidManifest.xml中註冊

在配置文件中註冊的接收者的特點是即使應用程序已被關閉,該接收者依然可接受它感興趣的廣播,比如手機電池電量的廣播接收者,沒有必要將某個程序開啓。下面的例子1、2廣播接收者會接收到撥打電話的廣播。


  1. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">

  2. <activityandroid:name=".MainActivity"

  3. android:label="@string/app_name">

  4. <intent-filter>

  5. <actionandroid:name="android.intent.action.MAIN"/>

  6. <categoryandroid:name="android.intent.category.LAUNCHER"/>

  7. </intent-filter>

  8. </activity>

  9. <!-- 廣播接收者1-->

  10. <receiverandroid:name=".BroadcastReceiver1">

  11. <intent-filter>

  12. <actionandroid:name="android.intent.action.CALL"></action>

  13. </intent-filter>

  14. </receiver>

  15. <!-- 廣播接收者2 -->

  16. <receiverandroid:name=".BroadcastReceiver2">

  17. <intent-filter>

  18. <actionandroid:name="android.intent.action.CALL"></action>

  19. </intent-filter>

  20. </receiver>

  21. <!-- 廣播接收者3 -->

  22. <receiverandroid:name=".BroadcastReceiver3">

  23. <intent-filter>

  24. <actionandroid:name="android.intent.action.PICK"></action>

  25. </intent-filter>

  26. </receiver>

  27. </application>


  1. /**

  2. * 模擬撥打電話廣播

  3. *  

  4. * @author 徐越

  5. *  

  6. */

  7. publicclass MainActivity extends Activity  

  8. {  

  9. @Override

  10. publicvoid onCreate(Bundle savedInstanceState)  

  11.    {  

  12. super.onCreate(savedInstanceState);  

  13.        setContentView(R.layout.main);  

  14.        Intent intent = new Intent();  

  15.        intent.setAction("android.intent.action.CALL");  

  16. this.sendBroadcast(intent);  

  17.    }  

  18. }  

  19. /**

  20. * 每次接收廣播都會生成新的BroadcastReceiver1,當處理完onReceive方法後就不會再被使用

  21. * 再次接收就在生成新的BroadcastReceiver1對象

  22. *  

  23. * @author 徐越

  24. *  

  25. */

  26. publicclass BroadcastReceiver1 extends android.content.BroadcastReceiver  

  27. {  

  28. public BroadcastReceiver1()  

  29.    {  

  30.        Log.i("xy_Receiver", "construtor1");  

  31.    }  

  32. @Override

  33. publicvoid onReceive(Context context, Intent intent)  

  34.    {  

  35.        Log.i("xy_Receiver", "onReceive1");  

  36.    }  

  37. }  

  38. /**

  39. * 廣播接收者2

  40. *  

  41. * @author 徐越

  42. *  

  43. */

  44. publicclass BroadcastReceiver2 extends android.content.BroadcastReceiver  

  45. {  

  46. public BroadcastReceiver2()  

  47.    {  

  48.        Log.i("xy_Receiver", "construtor2");  

  49.    }  

  50. @Override

  51. publicvoid onReceive(Context context, Intent intent)  

  52.    {  

  53.        Log.i("xy_Receiver", "onReceive2");  

  54.    }  

  55. }  

  56. /**

  57. * 廣播接收者3

  58. *  

  59. * @author 徐越

  60. *  

  61. */

  62. publicclass BroadcastReceiver3 extends android.content.BroadcastReceiver  

  63. {  

  64. public BroadcastReceiver3()  

  65.    {  

  66.        Log.i("xy_Receiver", "construtor3");  

  67.    }  

  68. @Override

  69. publicvoid onReceive(Context context, Intent intent)  

  70.    {  

  71.        Log.i("xy_Receiver", "onReceive3");  

  72.    }  

  73. }

②在Activity中註冊

在Activity中綁定接收者必須依附該應用程序存在,或者一個BroadcastReceiver用於更新UI,就沒有必要再程序關閉時接收者還運行,故無需在AndroidManifest.xml中註冊而可以放在Activity中註冊。


  1. /**

  2. * Activity中註冊廣播接收者

  3. *  

  4. * @author 徐越

  5. *  

  6. */

  7. publicclass MainActivity extends Activity  

  8. {  

  9. private BroadcastReceiver receiver;  

  10. privatestaticfinal String CALL_ACTION = "android.intent.action.CALL";  

  11. @Override

  12. publicvoid onCreate(Bundle savedInstanceState)  

  13.    {  

  14. super.onCreate(savedInstanceState);  

  15.        setContentView(R.layout.main);  

  16.    }  

  17. /**

  18.     * 模擬發送一個電話的廣播

  19.     *  

  20.     * @param v

  21.     */

  22. publicvoid sendBroadCast(View v)  

  23.    {  

  24.        Intent intent = new Intent();  

  25.        intent.setAction("android.intent.action.CALL");  

  26. this.sendBroadcast(intent);  

  27.    }  

  28. publicvoid bindReceiver(View v)  

  29.    {  

  30.        receiver = new BroadcastReceiver();  

  31.        IntentFilter intentFilter = new IntentFilter();  

  32.        intentFilter.addAction(CALL_ACTION);  

  33. this.registerReceiver(receiver, intentFilter);  

  34.    }  

  35. publicvoid unBindReceiver(View v)  

  36.    {  

  37. this.unregisterReceiver(receiver);  

  38.    }  

  39. }  

  40. publicclass BroadcastReceiver extends android.content.BroadcastReceiver  

  41. {  

  42. @Override

  43. publicvoid onReceive(Context context, Intent intent)  

  44.    {  

  45.        Log.i("xy", "receiver");  

  46.    }  

  47. }


本文出自 “IT徐胖子的專欄” 博客,請務必保留此出處http://woshixy.blog.51cto.com/5637578/1097197


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章