每一個應用程序均可以對本身感興趣的廣播進行註冊,這樣程序能夠接收到本身感興趣的廣播,這些廣播可能來自系統或其餘應用程序。java
一、註冊廣播的兩種方式:android
動態註冊:在代碼中註冊安全
//例如註冊監聽網絡變化的廣播 intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver,intentFilter); //在下面的本地廣播中也有動態註冊方法
靜態註冊:網絡
在AndroidManifest.xml中註冊app
相似<activity>標籤,android:name指定註冊的廣播接收器是哪個,<intent-filter>中的<action>標籤指定要接收的廣播類型異步
<application> <receiver android:name=".BootCompleteReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application>
二、廣播的兩種類型ide
<1>標準廣播,一種徹底異步執行的廣播,廣播發出後,廣播接收器幾乎會在同一時間接收到,無前後順序。this
<2>有序廣播spa
一種同步執行的廣播,在廣播發出後,同一時刻只會有一個廣播接收器接收到該廣播,此時廣播接收器是有前後順序的,而且能夠在<intent-filter android:priority = "">中指定優先級。而且優先級高的廣播接收器還能夠阻斷其餘廣播接收器。.net
<receiver android:name=".AnotherBroadcastReceiver"> <intent-filter android:priority="100"> <action android:name="sunny.example.broadcastreceivermy.MY_BROADCAST"/> </intent-filter> </receiver>
package sunny.example.broadcastreceiverorderedbroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AnotherBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ Toast.makeText(context, "received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show(); abortBroadcast();//阻斷比其優先級低的廣播 } }
三、發送自定義廣播
<1>發送標準廣播
//MainActivity.java package sunny.example.broadcastreceivermy; import android.support.v7.app.ActionBarActivity; import android.widget.Button; import android.os.Bundle; import android.view.View.OnClickListener; import android.view.View; import android.content.Intent; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button sendBroadcast = (Button)findViewById(R.id.sendBroadcast); sendBroadcast.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Intent intent = new Intent("sunny.example.broadcastreceivermy.MY_BROADCAST"); sendBroadcast(intent); //sendOrderedBroadcast(intent,null); } }); } } //自定義廣播接收器 package sunny.example.broadcastreceivermy; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show(); //abortBroadcast(); } }
//AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sunny.example.broadcastreceivermy" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="sunny.example.broadcastreceivermy.MY_BROADCAST"/> </intent-filter> </receiver> </application> </manifest>
<2>發送有序廣播
如上發送標準廣播中的//sendOrderedBroadcast(intent,null);,不一樣工程中的兩個廣播接收器能夠在其Manifest文件中設置優先級,使兩個接收器接收廣播有前後次序
三、使用本地廣播
以上發送與接收廣播都屬於系統全局廣播,任何應用程序均可以接收,有安全問題。Android中的本地廣播機制能夠使廣播只在程序內部傳遞。本地廣播只能動態註冊。
package sunny.example.broadcastreceiverlocal; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.ActionBarActivity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.IntentFilter; import android.os.Bundle; import android.widget.Button; import android.widget.Toast; import android.view.View.OnClickListener; import android.content.Intent; import android.view.View; public class MainActivity extends ActionBarActivity { private IntentFilter intentFilter; private LocalReceiver localReceiver; private LocalBroadcastManager localBroadcastManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取實例,以便下面調用LocalBroadcastManager的sendBroadcast()方法發送本地廣播 localBroadcastManager = LocalBroadcastManager.getInstance(this); Button localBroadcast = (Button)findViewById(R.id.localBroadcast); localBroadcast.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Intent intent = new Intent("sunny.example.broadcastreceiverlocal.LOCAL_BROADCAST"); localBroadcastManager.sendBroadcast(intent);//發送本地廣播 } }); //動態註冊本地廣播,本地廣播是沒法經過靜態註冊的方式接收的,靜態廣播是爲了讓程序在未啓動的狀況下也能收到廣播, //而發送本地廣播時,程序確定啓動了 intentFilter = new IntentFilter(); intentFilter.addAction("sunny.example.broadcastreceiverlocal.LOCAL_BROADCAST"); localReceiver = new LocalReceiver(); localBroadcastManager.registerReceiver(localReceiver, intentFilter);//註冊本地廣播接收器 //以上全在onCreate方法裏完成 } @Override public void onDestroy(){ super.onDestroy(); localBroadcastManager.unregisterReceiver(localReceiver);//取消註冊 } class LocalReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ Toast.makeText(context, "received local broadcast", Toast.LENGTH_LONG).show(); } } }