IncomingSMSRecevier receiver = new IncomingSMSRecevier();
registerReceiver(receiver, filter);
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/send"
android:text="点击发送广播"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class BroadCastMain extends Activity {
private Button sendBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendBtn = (Button) findViewById(R.id.send);
sendBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//设置intent的Action属性
intent.setAction("com.lp.action.WELCOME_BROADCAST");
intent.putExtra("msg", "消息");
//发送广播,使用intent发送一条广播
sendBroadcast(intent);
}
});
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/** 当符合该MyReceiver的广播出现时,onReceive方法会被触发,从而显示广播所携带的消息 */
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "接受到的Intent的Action是:"
+intent.getAction()
+"\n消息的内容为:"+intent.getStringExtra("msg")
, Toast.LENGTH_LONG).show();
}
}
<receiver android:name=".MyReceiver">
<intent-filter >
<action android:name="com.lp.action.WELCOME_BROADCAST"/>
</intent-filter>
</receiver>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击发送有序广播"
/>
</LinearLayout>
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BroadcastSorted extends Activity {
private Button sendbtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendbtn = (Button) findViewById(R.id.send);
sendbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.lp.action.WELCOME_BROADCAST");
intent.putExtra("msg", "消息");
//发送有序广播
sendOrderedBroadcast(intent, null);
}
});
}
}
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
Toast.makeText(context, "接收到的intent的action为:"+intent.getAction()
+"\nintent的消息的内容为:"+intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();
//创建一个Bundle对象
Bundle mBundle = new Bundle();
mBundle.putString("first", "第一个BroadcastReceiver存入的消息");
//将Bundle放入结果中
setResultExtras(mBundle);
//取消broadcast的继续传播
//abortBroadcast();
}
}
step4第二个BroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyReciever2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = getResultExtras(true);
//解析前一个BroadcastReceiver所存入的key为first的消息
String first = bundle.getString("first");
Toast.makeText(context, "第一个Broadcast存入的消息为:"+first,500000).show();
}
}
step5:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lp.ecjtu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BroadcastSorted"
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=".MyReceiver" >
<intent-filter android:priority="20">
<action android:name="com.lp.action.WELCOME_BROADCAST"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReciever2" >
<intent-filter android:priority="0">
<action android:name="com.lp.action.WELCOME_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>