您的位置:首页 > 其它

有序广播

2016-04-17 15:03 302 查看
有序广播是属于同步操作,广播发出后,只有等第一个广播接收到,并且运行完才会发送到下一个广播,并且可以拦截掉,使后面的广播接收者接收不到这条广播信息。

首先写两个广播接收者

广播接收者1:

public class MyBroadcast extends BroadcastReceiver{
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "广播1", 0).show();
//		停止有序广播的发送
abortBroadcast();
}
}

广播接收者2:

public class MyBroadcast02 extends BroadcastReceiver{
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "广播2", 0).show();
}
}

清单文件:

<receiver
            android:name="com.example.broadcastreceiver01.MyBroadcast"
            >
            <!-- 广播优先级 -->
            <intent-filter
                android:priority="101"
                >
                <action
                    android:name="aaa"
                    />
            </intent-filter>
        </receiver>
        <receiver android:name="com.example.broadcastreceiver01.MyBroadcast02">
            <intent-filter
                android:priority="100"
                >
                <action
                    android:name="aaa"
                    />
            </intent-filter>
         </receiver>

广播发送器:

Intent intent = new Intent("aaa");
sendOrderedBroadcast(intent, null);


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  四大组件