Android中BroadcastReceiver組件具體解釋

來源:互聯網
上載者:User

標籤:代碼   span   size   cas   ide   strong   非同步   state   track   

Android系統的4個組件最終還剩一種組件了BroadcastReceiver,這個組件是全域監聽器,能夠監聽系統全域的廣播訊息,能夠方便的實現系統中不同組件之間的通訊

BroadcastReceiver有自己的進程,系統級監聽器,僅僅要存在與之匹配的Intent被廣播出來,BroadcastReceiver就會被激發


要建立自己的BroadcastReceiver對象,我們須要繼承android.content.BroadcastReceiver,並實現其onReceive方法

MyReceiver.java

public class MyReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent){Toast.makeText(context,"接收到的Intent的Action為:" + intent.getAction() + "\n訊息內容是:" + intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();}}
Manifest.xml資訊清單檔配置的receiver

<receiver android:name=".MyReceiver"><intent-filter><action android:name="org.crazyit.action.CRAZY_BROADCAST" /></intent-filter></receiver>
就是說不管哪個組件中,intent的Action是"org.crazyit.action.CRAZY_BROADCAST" 並使用使用sendBroadcast(intent)發出廣播,那麼MyReceiver就會被啟動

public class BroadcastMain extends Activity{Button send;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 擷取程式介面中的buttonsend = (Button) findViewById(R.id.send);send.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){// 建立Intent對象Intent intent = new Intent();// 設定Intent的Action屬性intent.setAction("org.crazyit.action.CRAZY_BROADCAST");intent.putExtra("msg", "簡單的訊息");// 發送廣播sendBroadcast(intent);}});}}


注冊Receiver有兩種方法:

靜態注冊

靜態注冊是在AndroidManifest.xml檔案裡配置的。我們就來為MyReceiver注冊一個廣播位址:

<receiver android:name=".MyReceiver">              <intent-filter>                  <action android:name="android.intent.action.MY_BROADCAST"/>                  <category android:name="android.intent.category.DEFAULT" />              </intent-filter>          </receiver> 

配置了以上資訊之後。僅僅要是android.intent.action.MY_BROADCAST這個地址的廣播,MyReceiver都可以接收的到。注意,這樣的方式的注冊是常駐型的。也就是說當應用關閉後,假設有廣播資訊傳來,MyReceiver也會被系統調用而自己主動執行。

動態注冊

動態注冊須要在代碼中動態指定廣播位址並注冊,通常我們是在Activity或Service注冊一個廣播,以下我們就來看一下注冊的代碼:

MyReceiver receiver = new MyReceiver();            IntentFilter filter = new IntentFilter();  filter.addAction("android.intent.action.MY_BROADCAST");            registerReceiver(receiver, filter); //注冊<pre name="code" class="java">receiver 和filter


普通廣播

普通廣播對於多個接收者來說是全然非同步。通常每一個接收者都無需等待即能夠接收到廣播,接收者相互之間不會有影響。

對於這樣的廣播。接收者無法終止廣播。即無法阻止其它接收者的接收動作。

上面的範例就是發送的普通廣播

有序廣播

有序廣播比較特殊,它每次僅僅發送到優先順序較高的接收者那裡,然後由優先順序高的接受者再傳播到優先順序低的接收者那裡。優先順序高的接收者有能力終止這個廣播。

比如:優先順序A>B>C,Broadcast先傳給A。再傳給B,在傳給C。優先順序別聲明<Intent-filter>元素的android:priority中。數越大層級越高,取值範圍在-1000~1000

優先收到Broadcast的接受者能夠通過setResultExtras(Bundle)方法將處理結果存入Broadcast中,然後傳給下一個接受者。通過Bundle bunde=getResultExtras(true)柯獲得上一個接受者存入的資料

public class SortedBroadcast extends Activity{Button send;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 擷取程式中的sendbuttonsend = (Button) findViewById(R.id.send);send.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){// 建立Intent對象Intent intent = new Intent();intent.setAction("org.crazyit.action.CRAZY_BROADCAST");intent.putExtra("msg", "簡單的訊息");// 發送有序廣播sendOrderedBroadcast(intent, null);}});}}
MyReceiver.java

public class MyReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent){Toast.makeText(context,"接收到的Intent的Action為:" + intent.getAction() + "\n訊息內容是:"+ intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();// 建立一個Bundle對象,並存入資料Bundle bundle = new Bundle();bundle.putString("first", "第一個BroadcastReceiver存入的訊息");// 將bundle放入結果中setResultExtras(bundle);// 取消Broadcast的繼續傳播// abortBroadcast(); //①}}
MyReceiver2.java

public class MyReceiver2 extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent){Bundle bundle = getResultExtras(true);// 解析前一個BroadcastReceiver所存入的key為first的訊息String first = bundle.getString("first");Toast.makeText(context, "第一個Broadcast存入的訊息為:" + first, Toast.LENGTH_LONG).show();}}
資訊清單檔
<receiver android:name=".MyReceiver"><intent-filter android:priority="20"><action android:name="org.crazyit.action.CRAZY_BROADCAST" /></intent-filter></receiver><receiver android:name=".MyReceiver2"><intent-filter android:priority="0"><action android:name="org.crazyit.action.CRAZY_BROADCAST" /></intent-filter></receiver>




Android中BroadcastReceiver組件具體解釋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.