AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jing.example.nknu_.android_broadcast_order">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".One">
<intent-filter android:priority="999">
<action android:name="abc"/>
</intent-filter>
</receiver>
<receiver android:name=".Two">
<intent-filter android:priority="1000">
<action android:name="abc"/>
</intent-filter>
</receiver>
<receiver android:name=".Three">
<intent-filter android:priority="998">
<action android:name="abc"/>
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity
package com.jing.example.nknu_.android_broadcast_order;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("abc");
intent.putExtra("name","jack");
sendOrderedBroadcast(intent, null);
}
});
}
}
One
package com.jing.example.nknu_.android_broadcast_order;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by nknu_ on 2016/4/2.
*/
public class One extends BroadcastReceiver {
public One(){
}
public void onReceive(Context context, Intent intent){
String name = intent.getStringExtra("name");
System.out.println("---One->>" + name);
//試圖改變廣播值,但結果無法改變
intent.putExtra("name", "rose");
//消除廣播
//abortBroadcast();
}
}
Two
package com.jing.example.nknu_.android_broadcast_order;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by nknu_ on 2016/4/2.
*/
public class Two extends BroadcastReceiver {
public Two(){
}
public void onReceive(Context context, Intent intent){
String name = intent.getStringExtra("name");
System.out.println("---Two->>" + name);
}
}
Three
package com.jing.example.nknu_.android_broadcast_order;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by nknu_ on 2016/4/2.
*/
public class Three extends BroadcastReceiver {
public Three(){
}
public void onReceive(Context context, Intent intent){
String name = intent.getStringExtra("name");
System.out.println("---Three->>" + name);
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.jing.example.nknu_.android_broadcast_order.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發送有序廣播"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="183dp" />
</RelativeLayout>