From 77207b0b5f2091dae978f85ad4cd8bd76063ca29 Mon Sep 17 00:00:00 2001 From: wangzhichao Date: Mon, 21 Sep 2020 10:26:59 +0800 Subject: [PATCH] fix Receiver not registered: com.yanzhenjie.permission.bridge.Messenger --- .../permission/bridge/Messenger.java | 24 ++++++++++++++++--- .../permission/bridge/RequestExecutor.java | 16 +++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/permission/src/main/java/com/yanzhenjie/permission/bridge/Messenger.java b/permission/src/main/java/com/yanzhenjie/permission/bridge/Messenger.java index 1ac5b805..ee7763c1 100644 --- a/permission/src/main/java/com/yanzhenjie/permission/bridge/Messenger.java +++ b/permission/src/main/java/com/yanzhenjie/permission/bridge/Messenger.java @@ -34,6 +34,7 @@ public static void send(Context context, String suffix) { private final Context mContext; private final Callback mCallback; + private boolean mIsRegistered; public Messenger(Context context, Callback callback) { this.mContext = context; @@ -41,12 +42,29 @@ public Messenger(Context context, Callback callback) { } public void register(String suffix) { - IntentFilter filter = new IntentFilter(AndPermission.bridgeAction(mContext, suffix)); - mContext.registerReceiver(this, filter); + if (!mIsRegistered) { + IntentFilter filter = new IntentFilter(AndPermission.bridgeAction(mContext, suffix)); + mContext.registerReceiver(this, filter); + mIsRegistered = true; + } } + /* + Fix exception: + Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.yanzhenjie.permission.bridge.Messenger@c949a97 + at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:1471) + at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1571) + at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:664) + at com.yanzhenjie.permission.bridge.Messenger.unRegister(SourceFile:1) + at com.yanzhenjie.permission.bridge.RequestExecutor.onCallback(SourceFile:2) + at com.yanzhenjie.permission.bridge.Messenger.onReceive(SourceFile:1) + at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1620) + */ public void unRegister() { - mContext.unregisterReceiver(this); + if (mIsRegistered) { + mContext.unregisterReceiver(this); + mIsRegistered = false; + } } @Override diff --git a/permission/src/main/java/com/yanzhenjie/permission/bridge/RequestExecutor.java b/permission/src/main/java/com/yanzhenjie/permission/bridge/RequestExecutor.java index 5cc1609f..14ece3b6 100644 --- a/permission/src/main/java/com/yanzhenjie/permission/bridge/RequestExecutor.java +++ b/permission/src/main/java/com/yanzhenjie/permission/bridge/RequestExecutor.java @@ -41,15 +41,17 @@ public RequestExecutor(BridgeRequest request) { @Override public void run() { - Context context = mRequest.getSource().getContext(); + synchronized (this) { + Context context = mRequest.getSource().getContext(); - mMessenger = new Messenger(context, this); - mMessenger.register(getName()); + mMessenger = new Messenger(context, this); + mMessenger.register(getName()); - Intent intent = new Intent(); - intent.setAction(AndPermission.bridgeAction(context, null)); - intent.setPackage(context.getPackageName()); - context.bindService(intent, mConnection, Service.BIND_AUTO_CREATE); + Intent intent = new Intent(); + intent.setAction(AndPermission.bridgeAction(context, null)); + intent.setPackage(context.getPackageName()); + context.bindService(intent, mConnection, Service.BIND_AUTO_CREATE); + } } private ServiceConnection mConnection = new ServiceConnection() {