我创建了广播接收器,用于在服务到活动之间传递数据,当第一次打开app广播接收器一次叫oknly但是当app打开第二次然后广播接收器叫两次 - >将数据服务传递给
的代码 final Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.MyWebRequestReceiver.PROCESS_RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(BUNDLE, args);
sendBroadcast(broadcastIntent);
这里是活动和寄存器接收器中句柄响应的代码
IntentFilter filter = new IntentFilter(MyWebRequestReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MyWebRequestReceiver();
registerReceiver(receiver, filter);
registerReceiver(broadcastReceiver, new IntentFilter("broadCastName"));
处理其回复
public class MyWebRequestReceiver extends BroadcastReceiver {
public static final String PROCESS_RESPONSE = "com.as400samplecode.intent.action.PROCESS_RESPONSE";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
Bundle bundle = intent.getParcelableExtra(AutoOpenAppService.BUNDLE1);
String mainUrl = bundle.getString(MAINURL);
Boolean wvVisible = bundle.getBoolean(WVBOOLEAN);
Bundle args = intent.getParcelableExtra(AutoOpenAppService.BUNDLE);
ArrayList<? extends UrlTimeModel> list = args.getParcelableArrayList(LIST);
Long TimeOut = args.getLong(TIMEOUT);
if (mainUrl != null) {
if (!isFinishing()) {
setWebView(mainUrl);
}
}
Log.d(TAG, "VISIBLE : " + wvVisible);
if (wvVisible) {
webView.setVisibility(View.VISIBLE);
} else {
if (alert != null) {
alert.dismiss();
}
moveTaskToBack(true);
}
assert list != null;
if (list.size() < 0) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
alert.dismiss();
MainActivity.this.finish();
MainActivity.super.onBackPressed();
MainActivity.this.moveTaskToBack(true);
}
}, TimeOut);
}
LoadSubURl((ArrayList<UrlTimeModel>) list, TimeOut);
}
}
取消注册onstop()
@Override
protected void onStop() {
super.onStop();
if (timer != null && timerTask != null) {
timerTask.cancel();
timer.cancel();
timer.purge();
timer = null;
timerTask = null;
}
// TODO: 20/12/17 Call Hide Icon Method
HideIconMethod();
if (bound) {
unbindService(serviceConnection);
bound = false;
}
this.unregisterReceiver(receiver);
this.unregisterReceiver(broadcastReceiver);
}