Skip to content

Commit 335540e

Browse files
lrorthmannzxcpoiu
authored andcommitted
android: IllegalStateException when calling unregisterReceiver() (#72)
* Updated existing unregisterReceiver method * Catching possible exceptions that may occur when unregistering a receiver (like IllegalArgumentException)
1 parent e06cd60 commit 335540e

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

android/src/main/java/com/zxcpoiu/incallmanager/InCallManagerModule.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,7 @@ public void onReceive(Context context, Intent intent) {
309309
private void stopWiredHeadsetEvent() {
310310
if (wiredHeadsetReceiver != null) {
311311
Log.d(TAG, "stopWiredHeadsetEvent()");
312-
ReactContext reactContext = getReactApplicationContext();
313-
if (reactContext != null) {
314-
reactContext.unregisterReceiver(wiredHeadsetReceiver);
315-
} else {
316-
Log.d(TAG, "stopWiredHeadsetEvent() reactContext is null");
317-
}
312+
this.unregisterReceiver(this.wiredHeadsetReceiver);
318313
wiredHeadsetReceiver = null;
319314
}
320315
}
@@ -344,12 +339,7 @@ public void onReceive(Context context, Intent intent) {
344339
private void stopNoisyAudioEvent() {
345340
if (noisyAudioReceiver != null) {
346341
Log.d(TAG, "stopNoisyAudioEvent()");
347-
ReactContext reactContext = getReactApplicationContext();
348-
if (reactContext != null) {
349-
reactContext.unregisterReceiver(noisyAudioReceiver);
350-
} else {
351-
Log.d(TAG, "stopNoisyAudioEvent() reactContext is null");
352-
}
342+
this.unregisterReceiver(this.noisyAudioReceiver);
353343
noisyAudioReceiver = null;
354344
}
355345
}
@@ -416,12 +406,7 @@ public void onReceive(Context context, Intent intent) {
416406
private void stopMediaButtonEvent() {
417407
if (mediaButtonReceiver != null) {
418408
Log.d(TAG, "stopMediaButtonEvent()");
419-
ReactContext reactContext = getReactApplicationContext();
420-
if (reactContext != null) {
421-
reactContext.unregisterReceiver(mediaButtonReceiver);
422-
} else {
423-
Log.d(TAG, "stopMediaButtonEvent() reactContext is null");
424-
}
409+
this.unregisterReceiver(this.mediaButtonReceiver);
425410
mediaButtonReceiver = null;
426411
}
427412
}
@@ -1681,8 +1666,17 @@ private void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
16811666
}
16821667

16831668
/** Helper method for unregistration of an existing receiver. */
1684-
private void unregisterReceiver(BroadcastReceiver receiver) {
1685-
getReactApplicationContext().unregisterReceiver(receiver);
1669+
private void unregisterReceiver(final BroadcastReceiver receiver) {
1670+
final ReactContext reactContext = this.getReactApplicationContext();
1671+
if (reactContext != null) {
1672+
try {
1673+
reactContext.unregisterReceiver(receiver);
1674+
} catch (final Exception e) {
1675+
Log.d(TAG, "unregisterReceiver() failed");
1676+
}
1677+
} else {
1678+
Log.d(TAG, "unregisterReceiver() reactContext is null");
1679+
}
16861680
}
16871681

16881682
/** Sets the speaker phone mode. */

0 commit comments

Comments
 (0)