Fix ANR
This commit is contained in:
parent
9c090e62c8
commit
33869b3bfd
2 changed files with 12 additions and 4 deletions
|
|
@ -44,7 +44,7 @@ android {
|
||||||
vectorDrawables.useSupportLibrary = true
|
vectorDrawables.useSupportLibrary = true
|
||||||
|
|
||||||
applicationId 'com.kabouzeid.gramophone'
|
applicationId 'com.kabouzeid.gramophone'
|
||||||
versionCode 170
|
versionCode 172
|
||||||
versionName '1.3.0'
|
versionName '1.3.0'
|
||||||
}
|
}
|
||||||
signingConfigs {
|
signingConfigs {
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ import android.os.PowerManager.WakeLock;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
import com.kabouzeid.gramophone.BuildConfig;
|
import com.kabouzeid.gramophone.BuildConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -167,10 +169,16 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver {
|
||||||
private static void startService(Context context, String command) {
|
private static void startService(Context context, String command) {
|
||||||
final Intent intent = new Intent(context, MusicService.class);
|
final Intent intent = new Intent(context, MusicService.class);
|
||||||
intent.setAction(command);
|
intent.setAction(command);
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
try {
|
||||||
context.startForegroundService(intent);
|
// IMPORTANT NOTE: (kind of a hack)
|
||||||
} else {
|
// 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);
|
context.startService(intent);
|
||||||
|
} catch (IllegalStateException ignored) {
|
||||||
|
ContextCompat.startForegroundService(context, intent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue