无序广播与有序广播

一.普通广播(无序广播)

1.首先要了解一下无序无播的生命周期,API中写到广播生产周期,提供一个有效期间表示调用onReceive有效的,一但方法执行完毕对象finsh掉了, 可以在onReceive方法中做任何异步的操作。

2.使用自定义无序广播要有发送者设置一个意图setAction("xxx");广播接收者需在AndroidManifest.xml中过滤意图才能接收到广播。

代码如下:

广播的发送

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 发送广播,一个意图(普通无序的)
				Intent intent = new Intent(MainActivity.this,
						MyBrocastReceiver.class);
				intent.setAction("abc");
				intent.putExtra("name", "kaitao");
				sendBroadcast(intent);
			}
		});
	}

 广播的接收可以在onReceive中做任意的异步操作

//广播接收者
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String name = intent.getStringExtra("name");
		Toast.makeText(context, name, 1).show();
	}

广播的接收也可以做一个通知

	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		manager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		String name = intent.getStringExtra("name");
		// 做一个通知,不能在onReceive里面用AerltDialog
		NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
		builder.setContentTitle("没有网了");
		builder.setContentText("wifi掉了");
		builder.setTicker("广播来了");
		builder.setSmallIcon(R.drawable.ic_launcher);
		manager.notify(1001, builder.build());
	}

 AndroidManifest.xml设置

这里<receiver android:name=".MyBrocastReceiver2">是指定哪个类接收广播。
而 <action android:name="abc"/>则是过滤条件,   intent.setAction("abc");。

    <!-- 注册广播接收者 -->
        <receiver 
            android:name=".MyBrocastReceiver">
            <intent-filter>
                <action android:name="abc"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyBrocastReceiver2">
            <intent-filter>
                <action android:name="abc"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyBrocastReceiver3">
            <intent-filter>
                <action android:name="abc"/>
            </intent-filter>
        </receiver>

二、有序广播

使用有序广关健处在于定义接收者的优先权,以下代码中有三个类接收广播,

<intent-filter android:priority="1000">是定义接收的先后权限,priority值越大其优先权越大。

 <receiver android:name=".One">
            <intent-filter android:priority="900">
                <action android:name="abc"/>
            </intent-filter>
        </receiver>
        
         <receiver android:name=".Two">
            <intent-filter android:priority="1000">
                <action android:name="abc"/>
            </intent-filter>
        </receiver>
        
          <receiver android:name=".There">
            <intent-filter android:priority="800">
                <action android:name="abc"/>
            </intent-filter>
        </receiver>

 如果在接收者类中onReceive方法中,使用abortBroadcast();即终止广播,后面的广播不会在继续。

猜你喜欢

转载自kt-g.iteye.com/blog/2170923