廣播 BroadcastReceiver
監(jiān)聽 / 接收 廣播消息。Android不同組件間的通信(應(yīng)用內(nèi)或者不同應(yīng)用之間)
1.自定義接收器:繼承BroadcastReceivre類
public class WBroadcastReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent)
{
//通過Intent傳值區(qū)分不同的廣播和具體的傳參
}
}
1.1靜態(tài)注冊接收器
<receiver WBroadcastReceiver android:name=".WBroadcastReceiver " >
<intent-filter>
<action android:name="" />
</intent-filter>
</receiver>
1.2動態(tài)注冊接收器
//注冊
WBroadcastReceiver wBroadcastReceiver = new WBroadcastReceiver ();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("");
registerReceiver(wBroadcastReceiver , intentFilter);
//銷毀
unregisterReceiver(wBroadcastReceiver );
2.發(fā)送廣播
2.1普通廣播
Intent intent = new Intent();
intent.setAction("");
sendBroadcast(intent);
2.2系統(tǒng)廣播
即系統(tǒng)內(nèi)部的廣播
2.3有序廣播
即有順序的普通廣播
sendOrderedBroadcast(intent);
2.4本地廣播
//注冊
LocalBroadcastManager.getInstance(Context).registerReceiver(receiver, filter);
//銷毀
LocalBroadcastManager.getInstance(Context).unregisterReceiver(receiver, filter);
//發(fā)送
LocalBroadcastManager.getInstance(Context).sendBroadcast(intent);