安卓 广播BroadCast(广播接收与发送)

1、静态注册广播

package com.sc.broad;

import java.util.HashMap;

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

/* 需在AndroidManifest.xml添加广播静态配置,中添加需要监听的广播类型

...


    
        
        
        
        
        
    



*/

/** AndroidManifest.xml的静态注册广播(安装app时会自动注册,app未运行时也可正常接收广播 )
 * 
 * 用法:子类继承BroadCast,实现Broad(String action, Context context, Intent intent)对接收到的广播进行处理 */
public abstract class BroadCast extends BroadcastReceiver
{
	@Override
	public void onReceive(Context context, Intent intent)
	{
		String action = intent.getAction();
		Broad(action, context, intent);
	}
	
	/** 广播回调,action为广播类型 */
	public abstract void Broad(String action, Context context, Intent intent);
	
	
	// ------------
	
	/** 发送自定义广播,action广播名称 如:"com.sc.broad.actionDemo"; extData广播附带的数据; extName广播数据对应的名称 */
	public void SendBroadCast(Context context, String action, String extName, String extData)
	{
		BroadCastTool.SendBroadCast(context, action, extName, extData);
	}
	
	/** 发送自定义广播,action广播名称 如:"com.sc.broad.actionDemo"; ext为广播附带的数据 */
	public void SendBroadCast(Context context, String action, HashMap ext)
	{
		BroadCastTool.SendBroadCast(context, action, ext);
	}
}

示例:

package com.sc.broad;

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



/* 在AndroidManifest.xml添加,需要接收的广播静态配置(此处仅接收开机、锁屏广播)

....


    
        
        
        
        
        
    



*/

/** 广播接收示例 */
public class Broad1 extends BroadCast
{
	@Override
	public void Broad(String action, Context context, Intent intent)
	{
		Toast.makeText(context, "接收到广播" + action, Toast.LENGTH_SHORT).show();
		
		if(action.equals("android.intent.action.BOOT_COMPLETED"))
		{
			//....
		}
		else if(action.equals("android.intent.action.SCREEN_OFF"))
		{
			//....
		}
		else if(action.equals("com.sc.broad.actionDemo"))
		{
			String extName = "广播数据名称";
			String extData=intent.getStringExtra(extName);	// 获取自定义广播数据
			//....
		}
	}
	
}

2、动态监听广播


package com.sc.broad;

import java.util.HashMap;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


/** 代码动态监听广播(在context域范围内有效); 
 * 
 * 监听广播: BroadCastTool.Listen(Context context, CallBack call, String... actions) 
 * 取消监听: BroadCastTool.UnListen(Context context) 
 * 发送广播: SendBroadCast(Context context, String action,..)
 */
public class BroadCastTool extends BroadcastReceiver
{
	/** 广播消息接收回调逻辑 */
	public abstract class CallBack
	{
		/** 广播回调,action为广播类型 */
		public abstract void Broad(String action, Context context, Intent intent);
	}
	
	private boolean isregistered = false;
	Context context;
	CallBack call;
	
	/** 接收到的广播消息 */
	@Override
	public void onReceive(Context context, Intent intent)
	{
		String action = intent.getAction();
		if (call != null) call.Broad(action, context, intent);
	}
	
	/** 注册广播监听 */
	public void register(Context context, CallBack call, String... actions)
	{
		if (!isregistered)
		{
			this.context = context;
			this.call = call;
			
			// 注册广播接收器
			IntentFilter filter = new IntentFilter();
			for (String action : actions)
			{
				filter.addAction(action);
			}
			context.registerReceiver(this, filter);
			
			isregistered = true;
			// Toast.makeText(context, "ScreenReceiver 广播接收已启动 !", Toast.LENGTH_SHORT).show();
		}
	}
	
	/** 解除注册广播信息接收 */
	public void unRegister()
	{
		if (isregistered)
		{
			context.unregisterReceiver(this);
			isregistered = false;
			// Toast.makeText(context, "ScreenReceiver 广播接收已停止!", Toast.LENGTH_SHORT).show();
		}
	}
	
	// ------------
	static HashMap dic = new HashMap();
	
	/** 监听广播消息, context 监听广播的域; actions 广播类型如 { Intent.ACTION_SCREEN_ON Intent.ACTION_SCREEN_OFF Intent.ACTION_BOOT_COMPLETED }; call广播回调事件处理逻辑 */
	public static void Listen(Context context, CallBack call, String... actions)
	{
		if (!dic.containsKey(context))
		{
			BroadCastTool receiver = new BroadCastTool();
			receiver.register(context, call, actions);
			
			dic.put(context, receiver);
		}
	}
	
	/** 取消context对应的广播监听 */
	public static void UnListen(Context context)
	{
		if (dic.containsKey(context))
		{
			BroadCastTool receiver = dic.get(context);
			receiver.unRegister();
			
			dic.remove(context);
		}
	}
	
	// ------------
	
	/** 发送自定义广播,action广播名称 如:"com.sc.broad.actionDemo"; extData广播附带的数据; extName广播数据对应的名称 */
	public static void SendBroadCast(Context context, String action, String extName, String extData)
	{
		Intent intent = new Intent();
		intent.setAction(action);			// 设置广播的名字(设置Action)
		intent.putExtra(extName, extData);	// 广播附带的数据信息
		
		// String extData=intent.getStringExtra(extName); // 接收时调用
		
		context.sendBroadcast(intent);		// 发送广播(无序广播)
	}
	
	/** 发送自定义广播,action广播名称 如:"com.sc.broad.actionDemo"; ext为广播附带的数据 */
	public static void SendBroadCast(Context context, String action, HashMap ext)
	{
		Intent intent = new Intent();
		intent.setAction(action);			// 设置广播的名字(设置Action)
		
		// 添加广播附带的数据信息
		for (String key : ext.keySet())
		{
			intent.putExtra(key, ext.get(key));	
		}
		
		// String extData=intent.getStringExtra(extName); // 接收时调用
		
		context.sendBroadcast(intent);		// 发送广播(无序广播)
	}
}

 

你可能感兴趣的:(android,广播BroadCast)