Skip to content

Commit

Permalink
Fixed #880: Use explicit startForegroundService for Android 8+
Browse files Browse the repository at this point in the history
  • Loading branch information
daneren2005 committed Sep 1, 2018
1 parent 7f55a43 commit 0e97692
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private void getPlayOptions(Result<List<MediaBrowser.MediaItem>> result, String

public void getDownloadService() {
if(DownloadService.getInstance() == null) {
startService(new Intent(this, DownloadService.class));
DownloadService.startService(this);
}

waitForDownloadService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 0e97692

Please sign in to comment.