android-Broadcastrecriver

Base class for code that will receive intents sent by sendBroadcast().

If you don't need to send broadcasts across applications, consider using this class with LocalBroadcastManager instead of the more general facilities described below. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml.

Note:    If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister inActivity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.

註冊的兩種方式:

1.代碼動態註冊。

Activity.onResume() 中註冊,在Activity.onPause()解出註冊。不要再Activity.onSaveInstanceState()中解出註冊,因爲他不會被調用在用戶返回時。

private MyReceiver4 myReceiver4;
@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		IntentFilter intentFilter = new IntentFilter("abc");
		registerReceiver(myReceiver4, intentFilter); 
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(myReceiver4);
	}


2.AndroidManifest.xml清單文件裏 <receiver> 下聲明。

<receiver android:name="com.example.broadcast_receiver.MyReceiver" >
            <intent-filter android:priority="995">
                <action android:name="abc" />
            </intent-filter>
        </receiver>

android啓動activity是鏈式的,A->B ,B->C ,想將A的消息傳給C,就啓動廣播。

比如電池的變化,發送廣播,註冊廣播接收者。

廣播接收者的順序是無序的。


發送廣播有兩種方式:

1.normal broadcast  無序的

sendBroadcast(intent);

2.ordered broadcast 有序的

sendOrderedBroadcast(intent, receiverPermission);


There are two major classes of broadcasts that can be received:

  • Normal broadcasts (sent with Context.sendBroadcast) are completely asynchronous. All receivers of the broadcast are run in an undefined order, often at the same time. This is more efficient, but means that receivers cannot use the result or abort APIs included here.
  • 無序的異步傳送。所有的接收者的啓動時是無序的。這很高效,但是不能使用結果或者終止。
  • Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.
  • 有序的,一次傳一個。讓一個接收者返回時,可以傳送結果到下一個receiver,或者完全的終止廣播,使其不再繼續傳下去。接收者啓動的順序可以用 <intent-filter>android:priority來控制。 android:priority相同的話,隨機。用abortBroadcast()終止傳播。


生命週期

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active

就是執行 onReceive(Context, Intent)這段時間。

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

當函數返回後,生命週期結束。所以如果執行異步操作,可能在異步操作返回前就結束了。因此,不能再onReceive(Context, Intent)裏執行異步操作。

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use theNotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

尤其是,你不可能在BroadcastReceiver裏顯示dialog或者綁定service。前面的情況(異步),用通知實現。後面的情況(service),用startservice()開啓任務。



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