viewpager嵌套fragment或其他大量动态广播注册注销出现的崩溃
AndroidRuntime: FATAL EXCEPTION: main
Process: com.at.wallet, PID: 11556
android.app.RemoteServiceException: can't deliver broadcast
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1793)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6648)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:818)
主要的解决办法就是用LocalBroadcastManager.getInstance(mActivity)这个对象来注册广播。
//常量
Constants.java
public static final String BROADCAST_TRADE_INFO = "com.at.wallet.other.broadcast_trade_socket";
//判断重复注册
private boolean registTag = false;
//广播注册
if (broadcastReceiver != null && mActivity != null) {
IntentFilter myIntentFilter = new IntentFilter();
//长连接通知
myIntentFilter.addAction(Constants.BROADCAST_TRADE_INFO);
//注册广播
LocalBroadcastManager.getInstance(mActivity).registerReceiver(broadcastReceiver, myIntentFilter);
registTag = true;
}
//广播注销
if (broadcastReceiver != null && mActivity != null && registTag) {
LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(broadcastReceiver);
registTag = false;
}
//广播
/**
* 服务回调 长连接的数据
*/
public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (context != null && intent != null && intent.getAction() != null) {
if (intent.getAction().equals(Constants.BROADCAST_TRADE_INFO)) {
Log.e("操作tag","操作");
}
}
}
};
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
//服务或其他东东发送的广播
final Intent intent = new Intent(Constants.BROADCAST_TRADE_INFO);
Bundle bundle = new Bundle();
bundle.putSerializable(Constants.TRADE_INFO, list);
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);