diff --git a/app/build.gradle b/app/build.gradle index db2afc19..babf9d85 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,7 +44,7 @@ android { vectorDrawables.useSupportLibrary = true applicationId 'com.kabouzeid.gramophone' - versionCode 170 + versionCode 172 versionName '1.3.0' } signingConfigs { diff --git a/app/src/main/java/com/kabouzeid/gramophone/service/MediaButtonIntentReceiver.java b/app/src/main/java/com/kabouzeid/gramophone/service/MediaButtonIntentReceiver.java index 4cbe8457..0bab5868 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/service/MediaButtonIntentReceiver.java +++ b/app/src/main/java/com/kabouzeid/gramophone/service/MediaButtonIntentReceiver.java @@ -25,6 +25,8 @@ import android.os.PowerManager.WakeLock; import android.util.Log; import android.view.KeyEvent; +import androidx.core.content.ContextCompat; + import com.kabouzeid.gramophone.BuildConfig; /** @@ -167,10 +169,16 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver { private static void startService(Context context, String command) { final Intent intent = new Intent(context, MusicService.class); intent.setAction(command); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(intent); - } else { + try { + // IMPORTANT NOTE: (kind of a hack) + // on Android O and above the following crashes when the app is not running + // there is no good way to check whether the app is running so we catch the exception + // we do not always want to use startForegroundService() because then one gets an ANR + // if no notification is displayed via startForeground() + // according to Play analytics this happens a lot, I suppose for example if command = PAUSE context.startService(intent); + } catch (IllegalStateException ignored) { + ContextCompat.startForegroundService(context, intent); } }