Android菜鳥的成長筆記(26)——普通廣播與有序廣播

BroadcastReceiver是Android系統的四大組件之一,BroadcastReceiver是一個全局的系統級監聽器,它擁有自己的獨立進程。

我們來寫一個最簡單的廣播接收過程

先在manifest中定義一個廣播接受者

    
        
            
                

                
            
        
        
            
                
            
        
    

廣播接收者

public class MyBroadCast extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "接收到的值為:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
	}

}

向廣播接收者發送廣播

package com.example.broadcasttest;

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 MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setAction("com.meritit.action.MY_BROADCAST");
				intent.putExtra("msg", "陽光小強");
				sendBroadcast(intent);
			}
		});
	}
}

運行結果:

上面的例子是一個普通的廣播接受者,下面我們來修改一下代碼看看有序的廣播接受者

        
            
                
            
        
        
            
                
            
        

兩個廣播接收者設置瞭優先級,上面的優先級比下面的高

Intent intent = new Intent();
intent.setAction("com.meritit.action.MY_BROADCAST");
intent.putExtra("msg", "陽光小強");
sendOrderedBroadcast(intent, null);

發送有序廣播,註意是sendOrderedBroadcast

public class MyBroadCast extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "接收到的值為:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
		Bundle bundle = new Bundle();
		bundle.putString("msg2", "有序的廣播");
		setResultExtras(bundle);
		
		//如果不想繼續傳播
		//abortBroadcast();
	}

}

優先級高的MyBroadCast先接收到,有序廣播接收者可以添加新數據給下個等級的接受者。這種形式就有點像攔截器。

public class MyBroadCast2 extends BroadcastReceiver{

	private static final String TAG = "broadcast";

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = getResultExtras(true);
		String msg = intent.getStringExtra("msg");
		String msg2 = bundle.getString("msg2");
		Log.i(TAG, msg);
		Log.i(TAG, msg2);
	}

}

最後輸出結果:


除瞭接收用戶發送的廣播之外,BroadcastReceiver還有一個重要作用,就是接收系統廣播。

詳細請看:https://developer.android.com喎?/kf/ware/vc/” target=”_blank” class=”keylink”>vcmVmZXJlbmNlL2FuZHJvaWQvY29udGVudC9JbnRlbnQuaHRtbDwvcD48cD7PwsPmwdCz9kFuZHJvaWSzo7z7tcS547KloaM8L3A+PHA+PHVsPjxsaT48Y29kZT5BQ1RJT05fVElNRV9USUNLICAgICAgICAgICAgPC9jb2RlPjxsaT48Y29kZT5BQ1RJT05fVElNRV9DSEFOR0VEICAgICAgIM+1zbPKsbzksbu4xLHkPC9jb2RlPjxsaT48Y29kZT5BQ1RJT05fVElNRVpPTkVfQ0hBTkdFRCAgICAgz7XNs8qxx/ixu7jEseQ8L2NvZGU+PGxpPjxjb2RlPkFDVElPTl9CT09UX0NPTVBMRVRFRCAgICAgICAgz7XNs8b0tq/N6rPJPC9jb2RlPjxsaT48Y29kZT5BQ1RJT05fUEFDS0FHRV9BRERFRCAgICAgICAgIM+1zbPM7bzTsPw8L2NvZGU+PGxpPjxjb2RlPkFDVElPTl9QQUNLQUdFX0NIQU5HRUQgICAgICAgz7XNs7XEsPy4xLHkPC9jb2RlPjxsaT48Y29kZT5BQ1RJT05fUEFDS0FHRV9SRU1PVkVEICAgICAgICDPtc2ztcSw/LG7yb6z/TwvY29kZT48bGk+PGNvZGU+QUNUSU9OX1BBQ0tBR0VfUkVTVEFSVEVEICAgICAgz7XNs7XEsPyxu9bYxvQ8L2NvZGU+PGxpPjxjb2RlPkFDVElPTl9QQUNLQUdFX0RBVEFfQ0xFQVJFRCAgICAgIM+1zbO1xLD8yv2+3bG7x+W/1TwvY29kZT48bGk+PGNvZGU+QUNUSU9OX1VJRF9SRU1PVkVECQk8L2NvZGU+PGxpPjxjb2RlPkFDVElPTl9CQVRURVJZX0NIQU5HRUQgCQm157PYtefBv7jEseQ8L2NvZGU+PGxpPjxjb2RlPkFDVElPTl9QT1dFUl9DT05ORUNURUQgICAgICAgICAgz7XNs8GsvdO157PYPC9jb2RlPjxsaT48Y29kZT5BQ1RJT05fUE9XRVJfRElTQ09OTkVDVEVEICAgICAgIM+1zbPT67Xn1LS2z7+qPC9jb2RlPjxsaT48Y29kZT5BQ1RJT05fU0hVVERPV04gICAgICAgICAgICAgICAgz7XNs7G7udix1TwvY29kZT7A/cjnvOyy4rXns9i158G/uf21zTwvcD48cD48cHJlIGNsYXNzPQ==”brush:java;”>

public class MyBroadCast3 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		int current = bundle.getInt("level");
		int total = bundle.getInt("scale");
		if(current * 1.0 / total < 0.15){
			Toast.makeText(context, "電池電量過低,請盡快充電", Toast.LENGTH_LONG).show();
		}
	}

}

發佈留言