Ordered Broadcast有序广播

本文介绍了一个简单的有序广播实现案例,包括定义广播接收器、设置优先级及如何在Activity中发送有序广播。通过MainActivity发送带有自定义Action的Intent,MyReceiver1与MyReceiver按优先级顺序接收并处理广播。

sendBroadcast()发生无序广播

sendOrderedBroadcast()发送有序广播

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发生广播" />

</LinearLayout>


MainActivity

package com.example.orderbroadcast;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends Activity {

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

		send=(Button)this.findViewById(R.id.send);
		send.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setAction("dashu.orderedBroadcast");
				intent.putExtra("msg", "发送广播");
				sendOrderedBroadcast(intent, null);
			}
		});
	}
}


MyReceiver1

package com.example.orderbroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver1 extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "消息内容是"+intent.getStringExtra("msg"), Toast.LENGTH_LONG);
		//创建Bundle对象,并存入数据
		Bundle bundle=new Bundle();
		bundle.putString("frist", "第一个BroadcastReceiver消息...");
		//把bundle对象放到广播中传输
		setResultExtras(bundle);
		//取消Broadcast的继续传播
		//abortBroadcast();
	}

}


MyReceiver

package com.example.orderbroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Bundle bundle = getResultExtras(true);
		String frist = bundle.getString("frist");
		Toast.makeText(context, "第一个Broadcast存入的消息为:" + frist,
				Toast.LENGTH_LONG).show();
	}

}


 

Mainfest.xml

 <receiver android:name="com.example.orderbroadcast.MyReceiver" >
            <intent-filter android:priority="10" >
                <action android:name="dashu.orderedBroadcast" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.example.orderbroadcast.MyReceiver1" >
            <intent-filter android:priority="20" >
                <action android:name="dashu.orderedBroadcast" />
            </intent-filter>
        </receiver>
    </application>

android:priority设置接收广播优先级,取值范围-1000 到1000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值