Android知識點複習3(BroadcastReceiver)

超高校級的騷豬發表於2018-02-06

廣播 BroadcastReceiver

監聽 / 接收 廣播訊息。Android不同元件間的通訊(應用內或者不同應用之間)

1.自定義接收器:繼承BroadcastReceivre類

public class WBroadcastReceiver extends BroadcastReceiver {
   @Override public void onReceive(Context context, Intent intent) 
         { 
         //通過Intent傳值區分不同的廣播和具體的傳參
         }
     }
複製程式碼

1.1靜態註冊接收器

<receiver WBroadcastReceiver android:name=".WBroadcastReceiver " > 
   <intent-filter>
        <action android:name="" />
    </intent-filter>
</receiver>
複製程式碼

1.2動態註冊接收器

//註冊
WBroadcastReceiver wBroadcastReceiver = new WBroadcastReceiver ();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(""); 
registerReceiver(wBroadcastReceiver , intentFilter);
//銷燬
unregisterReceiver(wBroadcastReceiver );
複製程式碼

2.傳送廣播

2.1普通廣播

Intent intent = new Intent();
intent.setAction("");
sendBroadcast(intent);
複製程式碼

2.2系統廣播

即系統內部的廣播

2.3有序廣播

即有順序的普通廣播

sendOrderedBroadcast(intent);
複製程式碼

2.4本地廣播

//註冊
LocalBroadcastManager.getInstance(Context).registerReceiver(receiver, filter);
//銷燬
LocalBroadcastManager.getInstance(Context).unregisterReceiver(receiver, filter);
//傳送
LocalBroadcastManager.getInstance(Context).sendBroadcast(intent);
複製程式碼

3.未知,待補充。。。

相關文章