Android:广播接收器(BroadcastRecerver)

接收器需要继承BroadcastReseiver类,收到相应广播会执行其onReceive方法:

public class MyBroadcastReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "Receive the broadcast msg!...", Toast.LENGTH_SHORT).show();
		ConnectivityManager connManager = (ConnectivityManager)getSystemService(context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connManager.getActiveNetworkInfo();
		
		if(info != null && info.isAvailable()){
			Toast.makeText(context, "Available!...", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(context, "Not Available!...", Toast.LENGTH_SHORT).show();
		}
	}	
}

然后在AndroidManifest.xml里注册广播接收器,指定接收器接收广播类型。和Activity同级。

<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
        <action android:name="com.example.testbroadcast.CustomBroadcast"/>
    </intent-filter>
</receiver>

有些广播类型需要向android系统注册权限,在AndroidManifest.xml里注册,和<application>同级。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

应用程序可以发送标准广播和有序广播:

switch (v.getId()) {
	case R.id.button_stardard:
	  intent = new Intent("com.example.testbroadcast.CustomBroadcast");
		sendBroadcast(intent);
		break;
	case R.id.button_order:
		intent = new Intent("com.example.testbroadcast.CustomOrderBroadcast");
		sendOrderedBroadcast(intent, null);
		break;
	default:
		break;
}
<receiver android:name=".CustomBroadcastReceiverAnother">
   <intent-filter android:priority="100">
       <action android:name="com.example.testbroadcast.CustomOrderBroadcast"/>
   </intent-filter>
</receiver>

以上是静态接收广播,下面是动态接收广播:

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	IntentFilter filter = new IntentFilter();
	filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
	myBroadcastReceiver = new MyBroadcastReceiver();
	registerReceiver(myBroadcastReceiver, filter);
}

@Override
protected void onDestroy() {
	super.onDestroy();
	unregisterReceiver(myBroadcastReceiver);
}

本地广播:只能本应用程序发送和接收

public class Test extends Activity{
	private LocalBroadcastManager local;
	BroadcastReceiver receiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		
		local = LocalBroadcastManager.getInstance(this);
		receiver = new LocalReceiver();
		
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.test.LocalBroadcast");
		local.registerReceiver(receiver, filter);
		
		Intent intent = new Intent("com.test.LocalBroadcast");
		local.sendBroadcast(intent);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		local.unregisterReceiver(receiver);
	}

	class LocalReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(Test.this, "this is local broadcast!", Toast.LENGTH_SHORT).show();
		}
		
	}
}
posted @ 2019-01-03 13:55  xuejianbest  阅读(718)  评论(0编辑  收藏  举报