自定義廣播 學習筆記

定義廣播類

創建兩個測試廣播接收者類 CustomerBroadReveiver1 和 CustomerBroadReveiver2android

package com.tang.customerbroadcast;

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

public class CustomerBoradcast1 extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("廣播1");
		//Toast.makeText(context, "收到廣播了", Toast.LENGTH_SHORT).show();
	}
}

配置清單 (廣播優先級越大越先執行)

<receiver android:name=".CustomerBoradcast1">
		    <intent-filter android:priority="1">
		        <action android:name="android.customer.xxoo1"/>
		    </intent-filter>
		</receiver>
		<receiver android:name=".CustomerBroadcast2">
		    <intent-filter android:priority="10">
		        <action android:name="android.customer.xxoo1"/>
		    </intent-filter>
		</receiver>

測試發送廣播代碼

Intent it = new Intent();
it.setAction("android.customer.xxoo1");
//sendBroadcast(it); //每一個廣播接收者都會收到消息
//sendOrderedBroadcast(it, null);//有序發送,廣播接收者會按照優先級接受廣播事件
//第三個參數能夠在最後還能在執行一個廣播接收者
sendOrderedBroadcast(it, null, new FinalReceiver(), null, 0, null, null);

#FinalReceiver及自定以一個 廣播接收者類,最後執行他
相關文章
相關標籤/搜索