1. 程式人生 > >Ordered Broadcast有序廣播

Ordered Broadcast有序廣播

linear view 無序廣播 ear lis menu intent eight per

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

技術分享

Ordered Broadcast有序廣播