android廣播代碼彙總二__有序廣播

分析

普通廣播(Normal Broadcast):

一,優缺點:和有序廣播的優缺點相反!
二,發送廣播的方法:sendBroadcast()

有序廣播(Ordered Broadcast):

一,優缺點
優點:
1,按優先級的不同,優先Receiver可對數據進行處理,並傳給下一個Receiver
2,通過abortBroadcast可終止廣播的傳播

缺點:效率低
二,發送廣播的方法:sendOrderedBroadcast()
三,優先接收到Broadcast的Receiver可通過setResultExtras(Bundle)方法將處理結果存入Broadcast中,
下一個Receiver 通過 Bundle bundle=getResultExtras(true)方法獲取上一個 Receiver傳來的數據

程序效果:點擊按鈕,兩個Receiver接收同一條廣播,在logcat中打印出數據(按照Receiver的優先順序,Receiver2先,Receiver1後)
代碼

1 註冊廣播

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.song"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="8" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".C48_BroadcastActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!--優先級的設定 MyReceiver2大於MyReceiver1,優先級的範圍-1000~1000 -->
        </activity>
        <receiver android:name=".MyReceiver1">
            <intent-filter android:priority="200">
                <action android:name="com.song.123"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyReceiver2">
            <intent-filter android:priority="1000">
                <action android:name="com.song.123"/>
            </intent-filter>
        </receiver>
    </application>
 
</manifest>

2 接收廣播

package com.song;
//優先接到廣播,bundle綁上key爲b的數據
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
 
public class MyReceiver2 extends BroadcastReceiver{
 
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("receiver2");
//		context.getSystemService(name);
		Bundle bundle=intent.getExtras();
		bundle.putString("b", "bbb");
		System.out.println("a="+bundle.get("a"));
		setResultExtras(bundle);
		//切斷廣播
//		abortBroadcast();
	}
 
}

package com.song;
//接收從receiver2傳來的廣播,包含key爲a和b的數據
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
 
public class MyReceiver1 extends BroadcastReceiver{
 
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("receiver1");
		//要不要接受上一個廣播接收器receiver2傳來的的數據
		Bundle bundle=getResultExtras(true);
		System.out.println("a="+bundle.getString("a")+",b="+bundle.getString("b"));
	}
 
}

3 發送廣播

package com.song;
//發送廣播,bundle綁上key爲a的數據
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class C48_BroadcastActivity extends Activity {
    /** Called when the activity is first created. */
	Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent("com.song.123");
				Bundle bundle=new Bundle();
				bundle.putString("a", "aaa");
				intent.putExtras(bundle);
				//有序廣播
				sendOrderedBroadcast(intent, null);
			}
		});
        
    }
}

4 註銷廣播

 @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(localReceiver);
    }

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章