發送通知,更新UI或者數據,應用程序間相互通訊,監聽系統狀態(好比開機,網絡等)android
manifest清單文件中的全局註冊安全
按照生命週期,在Service或者Activity中使用代碼註冊網絡
manifest的註冊方式app
<receiver android:name="com.sample.test.MyBroadcastReciever"> <intent-filter> <action android:name="com.sample.test.ACTION_DO_SOMETHING"></action> <action android:name="android.intent.ACTION_WIFI_STATE_CHANGED"></action> </intent-filter> </receiver>
使用代碼註冊ide
SampleActivitythis
private MyReceiver receiver; @Override public void onStart() { super.onStart(); receiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.MY_BROADCAST"); registerReceiver(receiver, filter); } @Override public void onStop(){ super.onStop(); unregisterReceiver(receiver); }
Android中發送廣播的方式spa
普通廣播:不管優先級大小,將發送給全部監聽Action="com.test.sample.action"的廣播,內容不可被修改,無傳遞性。rest
Intent intent = new Intent( "com.test.sample.action"); sendBroadcast(intent);
黏性廣播code
當處理完以後的Intent ,依然存在,這時候registerReceiver(BroadcastReceiver, IntentFilter) 還能收到他的值,直到你把它去掉 , 無傳遞性 , 沒法終止(abort())廣播。xml
發這個廣播須要權限
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
去掉是用這個方法removeStickyBroadcast(intent); 但別忘了在執行這個方法的應用裏面 AndroidManifest.xml 一樣要加上面的權限;
sendStickyOrderedBroadcast(intent, resultReceiver, scheduler, initialCode, initialData, initialExtras)
有序廣播
按照接收者的優先級順序接收廣播 , 優先級別在 intent-filter 中的 priority 中聲明 ,-1000 到1000 之間 ,值越大 優先級越高 。能夠終止廣播意圖的繼續傳播 , 接收者能夠篡改內容,具備傳遞性。
sendOrderBroadcast(intent);
Android中的BroadcastReceiver能夠用來發送信息到另外一個廣播,這種方式可實現程序或者進程間的通行。
無序廣播
所謂無序廣播是指、發送廣播以後,接收順序是無序的,發送方式以下。
sendBroadcast(intent);
注意:無序廣播在某些狀況下仍是有序的,好比使用代碼註冊的的廣播優先級高、前臺app優先級更高、此外,無序廣播的接收順序和程序安裝順序也有必定的關係。
上面回顧了一下Android的廣播用例,整體來講安全性都不太好,所以只適用於安全性較低的數據傳遞,或者頁面更新。
在android-support-v4.jar中引入了LocalBroadcastManager,稱爲局部通知管理器,這種通知的好處是安全性高,效率也高,適合局部通訊,能夠用來代替Handler更新UI
public class LocalServiceBroadcasterActivity extends Activity { static final String ACTION_STARTED = "com.example.android.supportv4.STARTED"; static final String ACTION_UPDATE = "com.example.android.supportv4.UPDATE"; static final String ACTION_STOPPED = "com.example.android.supportv4.STOPPED"; LocalBroadcastManager mLocalBroadcastManager; BroadcastReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView callbackData = (TextView) findViewById(R.id.callback); callbackData.setText("No broadcast received yet"); mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_STARTED); filter.addAction(ACTION_UPDATE); filter.addAction(ACTION_STOPPED); mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_STARTED)) { callbackData.setText("STARTED"); } else if (intent.getAction().equals(ACTION_UPDATE)) { callbackData.setText("Got update: " + intent.getIntExtra("value", 0)); } else if (intent.getAction().equals(ACTION_STOPPED)) { callbackData.setText("STOPPED"); } } }; mLocalBroadcastManager.registerReceiver(mReceiver, filter); Button button = (Button) findViewById(R.id.start); button.setOnClickListener(mStartListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); } @Override protected void onDestroy() { super.onDestroy(); mLocalBroadcastManager.unregisterReceiver(mReceiver); } private OnClickListener mStartListener = new OnClickListener() { public void onClick(View v) { startService(new Intent(LocalServiceBroadcasterActivity.this, LocalService.class)); } }; private OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { stopService(new Intent(LocalServiceBroadcasterActivity.this, LocalService.class)); } }; public static class LocalService extends Service { LocalBroadcastManager mLocalBroadcastManager; int mCurUpdate; static final int MSG_UPDATE = 1; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_UPDATE: { mCurUpdate++; Intent intent = new Intent(ACTION_UPDATE); intent.putExtra("value", mCurUpdate); mLocalBroadcastManager.sendBroadcast(intent); Message nmsg = mHandler.obtainMessage(MSG_UPDATE); mHandler.sendMessageDelayed(nmsg, 1000); } break; default: super.handleMessage(msg); } } }; @Override public void onCreate() { super.onCreate(); mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); } public int onStartCommand(Intent intent, int flags, int startId) { // Tell any local interested parties about the start. mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STARTED)); // Prepare to do update reports. mHandler.removeMessages(MSG_UPDATE); Message msg = mHandler.obtainMessage(MSG_UPDATE); mHandler.sendMessageDelayed(msg, 1000); return ServiceCompat.START_STICKY; } @Override public void onDestroy() { super.onDestroy(); // Tell any local interested parties about the stop. mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STOPPED)); // Stop doing updates. mHandler.removeMessages(MSG_UPDATE); } @Override public IBinder onBind(Intent intent) { return null; } } }