自己发广播,自己接收广播 2.有序广播

有序广播即存在先后顺序

在MyReceiver的activity_main.xml中创建一个Button

 <Button 
        android:id="@+id/btn_receiver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="有序广播"/>

MainActivity.java(广播内容是“习大大给每家每户发放一百斤大米”,广播顺序是省,市,农民)

package com.briup.myrecevier;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	private Button btn_receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn_receiver = (Button) findViewById(R.id.btn_receiver);
		btn_receiver.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_receiver:// 有序广播
			Intent intent2 = new Intent();
			intent2.setAction("com.briup.rice");// 广播的波频
			/*
			 * sendOrderedBroadcast(intent2, 权限, 返回广播,
			 * scheduler, 接收码, 内容, initialExtras);
			 */
			// 发送有序广播
			sendOrderedBroadcast(intent2, null, null, null, 1,
					"习大大给每家每户发放一百斤大米", null);
			break;
		default:
			break;
		}
	}
}

在Receiver中

ProviderReceiver.java(省)

package com.briup.receiver;

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

public class ProviderReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// 获取有序广播消息
		String rice = getResultData();
		Toast.makeText(context,"省:"+rice, Toast.LENGTH_LONG).show();
		setResultData("习大大说每家每户发放五十斤大米");
		//中断广播
		//abortBroadcast();
	}
}

CityReceiver.java(市)

package com.briup.receiver;

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

public class CityReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		String rice = getResultData();
		Toast.makeText(context,"市:"+rice,Toast.LENGTH_LONG).show();
		setResultData("习大大说每家每户发放十斤大米");
	}
}

FarmReceiver.java(农民)

package com.briup.receiver;

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

public class FarmReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		String rice = getResultData();
		Toast.makeText(context,"农民:"+rice,Toast.LENGTH_LONG).show();
		
	}
}

在配置文件中注册,3个都要注册,注意优先级的值是随意的,大小合适要有次序

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.briup.receiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <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=".ProviderReceiver">
            <!-- android:priority="1000"设置优先级 -->
            <intent-filter android:priority="1000">
                <action android:name="com.briup.rice"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".CityReceiver">
            <intent-filter android:priority="200">
                <action android:name="com.briup.rice"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".FarmReceiver">
            <intent-filter android:priority="10">
                <action android:name="com.briup.rice"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

先运行Receiver,再运行MyReceiver,效果如下


如果想让发送广播的一方再收到广播最后的结果,首先要在MyReceiver创建一个FinalReceiver.java

package com.briup.myrecevier;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
//最后一个返回广播不需要配置注册
public class FinalReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		String rice = getResultData();
		Toast.makeText(context,"秘书:"+rice, Toast.LENGTH_LONG).show();
	}	
}

并将MainActivity.java修改sendOrderedBroadcast第三个参数

case R.id.btn_receiver:// 有序广播
			Intent intent2 = new Intent();
			intent2.setAction("com.briup.rice");// 广播的波频
			/*
			 * sendOrderedBroadcast(intent2, 权限, 返回广播,
			 * scheduler, 接收码, 内容, initialExtras);
			 */
			// 发送有序广播(需要返回值(秘书说的时候)把第三个参数由null改为new FinalReceiver())
			sendOrderedBroadcast(intent2, null, new FinalReceiver(), null, 1,
					"习大大给每家每户发放一百斤大米", null);
			break;

再运行,效果如下:




阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页