From 9109204bbc34a42dd1a720cd341eb7a2a775d7cf Mon Sep 17 00:00:00 2001 From: Glenn Guy Date: Sat, 13 Oct 2018 22:41:47 +1100 Subject: Fix again registering/unregistering AudioNoisyReceiver Moved to onCreate()/onDestroy() rather than start() stop() and pause() - receiver would not be registered in some cases causing and exception to occur and audio playback to continue when bluetooth is disconnected. --- .../java/github/daneren2005/dsub/service/DownloadService.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java index f4bc2343..9a0f606e 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -183,7 +183,7 @@ public class DownloadService extends Service { private boolean runListenersOnInit = false; private IntentFilter audioNoisyIntent = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY); - private AudioNoisyReceiver audioNoisyReceiver = new AudioNoisyReceiver(); + private AudioNoisyReceiver audioNoisyReceiver = null; private MediaRouteManager mediaRouter; @@ -267,6 +267,8 @@ public class DownloadService extends Service { }, "DownloadService").start(); Util.registerMediaButtonEventReceiver(this); + audioNoisyReceiver = new AudioNoisyReceiver(); + registerReceiver(audioNoisyReceiver, audioNoisyIntent); if (mRemoteControl == null) { // Use the remote control APIs (if available) to set the playback state @@ -380,6 +382,9 @@ public class DownloadService extends Service { proxy.stop(); proxy = null; } + if (audioNoisyReceiver != null) { + unregisterReceiver(audioNoisyReceiver); + } mediaRouter.destroy(); Notifications.hidePlayingNotification(this, this, handler); Notifications.hideDownloadingNotification(this, this, handler); @@ -1333,7 +1338,6 @@ public class DownloadService extends Service { } else if(playerState == PAUSED_TEMP) { setPlayerState(temp ? PAUSED_TEMP : PAUSED); } - unregisterReceiver(audioNoisyReceiver); } catch (Exception x) { handleError(x); } @@ -1358,7 +1362,6 @@ public class DownloadService extends Service { } else if(playerState == PAUSED) { setPlayerState(STOPPED); } - unregisterReceiver(audioNoisyReceiver); } catch(Exception x) { handleError(x); } @@ -1378,7 +1381,6 @@ public class DownloadService extends Service { autoPlayStart = true; } } - registerReceiver(audioNoisyReceiver, audioNoisyIntent); setPlayerState(STARTED); } catch (Exception x) { handleError(x); -- cgit v1.2.3