BroadcastReceive廣播接收器:,
BroadcastReceive廣播接收器:public class Test extends Activity{ private final String ACTION_NAME = "發送廣播"; private Button mBtnMsgEvent = null; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //註冊廣播 registerBoradcastReceiver(); LinearLayout mLinearLayout = new LinearLayout(this); mBtnMsgEvent = new Button(this); mBtnMsgEvent.setText("發送廣播"); mLinearLayout.addView(mBtnMsgEvent); setContentView(mLinearLayout); mBtnMsgEvent.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent mIntent = new Intent(ACTION_NAME); mIntent.putExtra("yaner", "發送廣播,相當於在這裡傳送資料"); //發送廣播 sendBroadcast(mIntent); } }); } private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(ACTION_NAME)){ Toast.makeText(Test.this, "處理action名字相對應的廣播", 200); } } }; public void registerBoradcastReceiver(){ IntentFilter myIntentFilter = new IntentFilter(); myIntentFilter.addAction(ACTION_NAME); //註冊廣播 registerReceiver(mBroadcastReceiver, myIntentFilter); } } //切記關閉廣播protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(receiver); }你的應用可以使用它對外來事件進行過濾只對感興趣的外來事件(如當電話呼入時,或者資料網路可用時)進行接收並做出響應。廣播接收器沒有使用者介面。然而,它們可以啟動一個activity或serice 來響應它們收到的資訊,或者用NotificationManager 來通知使用者。通知可以用很多種方式來吸引使用者的注意力──閃動背燈、震動、播放聲音等。一般來說是在狀態列上放一個持久的表徵圖,使用者可以開啟它並擷取訊息。廣播類型:普通廣播,通過Context.sendBroadcast(Intent myIntent)發送的有序廣播,通過Context.sendOrderedBroadcast(intent, receiverPermission)發送的,該方法第2個參數決定該廣播的層級,層級數值是在 -1000 到 1000 之間 , 值越大 , 發送的優先順序越高;廣播接收者接收廣播時的層級層級(可通過intentfilter中的priority進行設定設為2147483647時優先順序最高),同層級接收的先後是隨機的, 再到層級低的收到廣播,進階別的或同層級先接收到廣播的可以通過abortBroadcast()方法截斷廣播使其他的接收者無法收到該廣播,還有其他建構函式非同步廣播,通過Context.sendStickyBroadcast(Intent myIntent)發送的,還有sendStickyOrderedBroadcast(intent, resultReceiver, scheduler, initialCode, initialData, initialExtras)方法,該方法具有有序廣播的特性也有非同步廣播的特性;發送非同步廣播要: <uses-permission android:name="android.permission.BROADCAST_STICKY" />許可權,接收並處理完Intent後,廣播依然存在,直到你調用removeStickyBroadcast(intent)主動把它去掉注意:發送廣播時的intent參數與Contex.startActivity()啟動起來的Intent不同,前者可以被多個訂閱它的廣播接收器調用,後者只能被一個(Activity或service)調用監聽廣播Intent步驟:1> 寫一個繼承BroadCastReceiver的類,重寫onReceive()方法,廣播接收器僅在它執行這個方法時處於活躍狀態。當onReceive()返回後,它即為失活狀態,注意:為了保證使用者互動過程的流暢,一些費時的操作要放到線程裡,如類名SMSBroadcastReceiver2> 註冊該廣播接收者,註冊有兩種方法程式動態註冊和AndroidManifest檔案中進行靜態註冊(可理解為系統中註冊)如下: 靜態註冊,註冊的廣播,下面的priority表示接收廣播的層級"2147483647"為最高優先順序<receiver android:name=".SMSBroadcastReceiver"><intent-filter android:priority = "2147483647"><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver >動態註冊,一般在Activity可互動時onResume()內註冊BroadcastReceiverIntentFilter intentFilter=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");registerReceiver(mBatteryInfoReceiver ,intentFilter);//反註冊unregisterReceiver(receiver);注意:1.生命週期只有十秒左右,如果在 onReceive() 內做超過十秒內的事情,就會報ANR(Application No Response) 程式無響應的錯誤資訊,如果需要完成一項比較耗時的工作 , 應該通過發送 Intent 給 Service, 由Service 來完成 . 這裡不能使用子線程來解決 , 因為 BroadcastReceiver 的生命週期很短 , 子線程可能還沒有結束BroadcastReceiver 就先結束了 .BroadcastReceiver 一旦結束 , 此時 BroadcastReceiver 的所在進程很容易在系統需要記憶體時被優先殺死 , 因為它屬於空進程 ( 沒有任何活動組件的進程 ). 如果它的宿主進程被殺死 , 那麼正在工作的子線程也會被殺死 . 所以採用子線程來解決是不可靠的2. 動態註冊廣播接收器還有一個特點,就是當用來註冊的Activity關掉後,廣播也就失效了。靜態註冊無需擔憂廣播接收器是否被關閉,只要裝置是開啟狀態,廣播接收器也是開啟著的。也就是說哪怕app本身未啟動,該app訂閱的廣播在觸發時也會對它起作用系統常見廣播Intent,如開機啟動、電池電量變化、時間改變等廣播