diff --git a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java index a1c5ceef1..5948064d1 100644 --- a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java +++ b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java @@ -144,7 +144,7 @@ protected void onCreate(Bundle bundle) { applyTheme(); applyFullscreen(); super.onCreate(bundle); - startService(new Intent(this, DownloadService.class)); + DownloadService.startService(this); setVolumeControlStream(AudioManager.STREAM_MUSIC); if(getIntent().hasExtra(Constants.FRAGMENT_POSITION)) { @@ -1003,7 +1003,7 @@ public DownloadService getDownloadService() { break; } Log.w(TAG, "DownloadService not running. Attempting to start it."); - startService(new Intent(this, DownloadService.class)); + DownloadService.startService(this); Util.sleepQuietly(50L); } diff --git a/app/src/main/java/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java b/app/src/main/java/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java index 041eb8e1a..ebf509362 100644 --- a/app/src/main/java/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java +++ b/app/src/main/java/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java @@ -33,7 +33,7 @@ public void onReceive(Context context, Intent intent) { if(headphoneState == 1 && Util.shouldStartOnHeadphones(context)) { Intent start = new Intent(context, DownloadService.class); start.setAction(DownloadService.START_PLAY); - context.startService(start); + DownloadService.startService(context, start); } } } diff --git a/app/src/main/java/github/daneren2005/dsub/receiver/MediaButtonIntentReceiver.java b/app/src/main/java/github/daneren2005/dsub/receiver/MediaButtonIntentReceiver.java index 8119ef2d3..0f0508019 100644 --- a/app/src/main/java/github/daneren2005/dsub/receiver/MediaButtonIntentReceiver.java +++ b/app/src/main/java/github/daneren2005/dsub/receiver/MediaButtonIntentReceiver.java @@ -45,7 +45,7 @@ public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(context, DownloadService.class); serviceIntent.putExtra(Intent.EXTRA_KEY_EVENT, event); - context.startService(serviceIntent); + DownloadService.startService(context, serviceIntent); if (isOrderedBroadcast()) { try { abortBroadcast(); diff --git a/app/src/main/java/github/daneren2005/dsub/receiver/PlayActionReceiver.java b/app/src/main/java/github/daneren2005/dsub/receiver/PlayActionReceiver.java index 2c04d8293..aa3b9173b 100644 --- a/app/src/main/java/github/daneren2005/dsub/receiver/PlayActionReceiver.java +++ b/app/src/main/java/github/daneren2005/dsub/receiver/PlayActionReceiver.java @@ -40,7 +40,7 @@ public void onReceive(Context context, Intent intent) { start.putExtra(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, data.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR)); start.putExtra(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, data.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE)); start.putExtra(Constants.PREFERENCES_KEY_OFFLINE, data.getInt(Constants.PREFERENCES_KEY_OFFLINE)); - context.startService(start); + DownloadService.startService(context, start); } } } diff --git a/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java b/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java index d4e544c61..e4edabc42 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java @@ -524,7 +524,7 @@ private void getPlayOptions(Result> result, String public void getDownloadService() { if(DownloadService.getInstance() == null) { - startService(new Intent(this, DownloadService.class)); + DownloadService.startService(this); } waitForDownloadService(); 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 a4ca705cf..0c794c360 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -71,6 +71,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import android.annotation.TargetApi; +import android.app.Activity; import android.app.Service; import android.content.ComponentCallbacks2; import android.content.ComponentName; @@ -375,6 +376,17 @@ public void onDestroy() { Notifications.hideDownloadingNotification(this, this, handler); } + public static void startService(Context context) { + startService(context, new Intent(context, DownloadService.class)); + } + public static void startService(Context context, Intent intent) { + PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE); + if (Build.VERSION.SDK_INT < 26 || (powerManager != null && powerManager.isIgnoringBatteryOptimizations(intent.getPackage()))) { + context.startService(intent); + } else { + context.startForegroundService(intent); + } + } public static DownloadService getInstance() { return instance; }