记录一下在项目中动态使用广播的方法
private SpeechReceiver receiver; /** * 注册动态广播接收极光推送的信息 */ public static final String ScanAction = "com.xgs.ui.scan"; public class SpeechReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
}
}
在当前页面创建的时候注册这个广播
receiver = new SpeechReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(ScanAction); registerReceiver(receiver, filter);
在发送广播的位置
Intent intents = new Intent(注册广播的class.ScanAction); intents.putExtra("ssa", ssa); context.sendBroadcast(intents);
intent 可传递数据
在结束页面的时候回收
if (null != receiver) { unregisterReceiver(receiver); }
就可以了