每个应用程序都可以对自己感兴趣的广播进行注册,这样程序可以接收到自己感兴趣的广播,这些广播可能来自系统或其他应用程序。
1、注册广播的两种方式:
动态注册:在代码中注册
//例如注册监听网络变化的广播 intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver,intentFilter); //在下面的本地广播中也有动态注册方法
静态注册:
在AndroidManifest.xml中注册
类似<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>
2、广播的两种类型
<1>标准广播,一种完全异步执行的广播,广播发出后,广播接收器几乎会在同一时间接收到,无先后顺序。
<2>有序广播
一种同步执行的广播,在广播发出后,同一时刻只会有一个广播接收器接收到该广播,此时广播接收器是有先后顺序的,并且可以在<intent-filter android:priority = "">中指定优先级。并且优先级高的广播接收器还可以阻断其他广播接收器。
<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();//阻断比其优先级低的广播 } }
3、发送自定义广播
<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文件中设置优先级,使两个接收器接收广播有先后次序
3、使用本地广播
以上发送与接收广播都属于系统全局广播,任何应用程序都可以接收,有安全问题。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(); } } }