Android的BroadcastReceiver简介 (二)

2014-11-24 10:19:01 · 作者: · 浏览: 1
();
intent.setAction("com.home.Sort_BroadCast");
intent.putExtra("msg", "发有序广播啦!!!");
sendOrderedBroadcast(intent, null);
}
});
}
}

FristReceiver:

[java]
package com.home.receiver;

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

public class FristReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// 如果收到的是普通广播
if ("com.home.Common_BroadCast".equals(intent.getAction())) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, "FristReceiver收到的普通广播为:" + msg,
Toast.LENGTH_SHORT).show();
}
// 如果收到的是有序广播
if ("com.home.Sort_BroadCast".equals(intent.getAction())) {
String msg = intent.getStringExtra("msg");
// 取出上一个广播设置的数据
Bundle bundle = getResultExtras(true);
String PassMsg = "";
if (bundle != null) {
PassMsg = bundle.getString("message");
}
String resultMsg = msg + "\n" + PassMsg;
Toast.makeText(context, "FristReceiver收到的有序广播为:" + resultMsg,
Toast.LENGTH_SHORT).show();
}

}

}

package com.home.receiver;

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

public class FristReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// 如果收到的是普通广播
if ("com.home.Common_BroadCast".equals(intent.getAction())) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, "FristReceiver收到的普通广播为:" + msg,
Toast.LENGTH_SHORT).show();
}
// 如果收到的是有序广播
if ("com.home.Sort_BroadCast".equals(intent.getAction())) {
String msg = intent.getStringExtra("msg");
// 取出上一个广播设置的数据
Bundle bundle = getResultExtras(true);
String PassMsg = "";
if (bundle != null) {
PassMsg = bundle.getString("message");
}
String resultMsg = msg + "\n" + PassMsg;
Toast.makeText(context, "FristReceiver收到的有序广播为:" + resultMsg,
Toast.LENGTH_SHORT).show();
}

}

}
SecondReceiver:

[java]
package com.home.receiver;

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

public class SecondReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// 如果收到的是普通广播
if ("com.home.Common_BroadCast".equals(intent.getAction())) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, "SecondReceiver收到的普通广播为:" + msg,
Toast.LENGTH_SHORT).show();
}
// 如果收到的是有序广播
if ("com.home.Sort_BroadCast".equals(intent.getAction())) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, "SecondReceiver收到的有序广播为:" + msg,
Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();