After upgrading my phone to 8.1 Developer Preview my background service no longer starts up properly. I still see a difference, in android oreo I don't see my custom foreground notification (I only see the "app is running in the background" notification). It works on android < 26 and on android 26 (Oreo) as well. Do I have to adjust anything there as well? Tks Bro!
My Service:
public class ForegroundService extends Service {
private static final String LOG_TAG = "ForegroundService";
public static boolean IS_SERVICE_RUNNING = false;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
showNotification();
} else if (intent != null && intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
MainActivity.exoPlayer.setPlayWhenReady(false);
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
private void showNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Intent playIntent = new Intent(this, ForegroundService.class);
playIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
playIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.radio);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Background")
.setContentText("is Playing...")
.setSmallIcon(R.drawable.background)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_delete, "Turn Off",
pplayIntent).build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// Used only in case if services are bound (Bound Services).
return null;
}
}
My Constants:
public class Constants {
public interface ACTION {
public static String MAIN_ACTION = "com.marothiatechs.foregroundservice.action.main";
public static String PLAY_ACTION = "com.marothiatechs.foregroundservice.action.play";
public static String STARTFOREGROUND_ACTION = "com.marothiatechs.foregroundservice.action.startforeground";
public static String STOPFOREGROUND_ACTION = "com.marothiatechs.foregroundservice.action.stopforeground";
}
public interface NOTIFICATION_ID {
public static int FOREGROUND_SERVICE = 101;
}
}