Foreground Service doesn't run in Oreo












3














I have a foreground service that runs on my phone(Android 7.0) but it doesn't run on a friends phone(Android 8.0).I also test it on android studio emulators and i have the same problem on version 8.0.I've implemented the if-else check to see if it's Android 8.0 so i can call startForegroundService() and still nothing.



Here is how i call it:



 //Start foreground service
Intent intent = new Intent(this, MyForeGroundService.class);
intent.setAction(MyForeGroundService.ACTION_START_FOREGROUND_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
}else{
startService(intent);
}


And here is my foreground service:



public class MyForeGroundService extends Service implements LocationListener {

public MyForeGroundService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();
wakeLock();
}

@SuppressLint("InvalidWakeLockTag")
public void wakeLock() {
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
assert pm != null;
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "partialwl");
wl.acquire();
}

public void stopWakeLock() {
wl.release();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();

switch (action) {
case ACTION_START_FOREGROUND_SERVICE:
wakeLock();
startForegroundService();
break;
case ACTION_STOP_FOREGROUND_SERVICE:
stopWakeLock();
stopForegroundService();
break;
case ACTION_PLAY:
Intent openMap= new Intent(this,Map.class);
startActivity(openMap);
break;
case ACTION_PAUSE:
break;
}
}
return START_STICKY;
}

/* Used to build and start foreground service. */
private void startForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Start foreground service.");

// Create notification default intent.
Intent intent = new Intent(this,Map.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

// Create notification builder.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

//Go back to Map activity if user press at the notification
builder.setContentIntent(pendingIntent)
.setContentTitle("gEKOning...")
.setContentText("Tap to open gEKOn app");


builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.mipmap.ic_launcher);
Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(largeIconBitmap);
// Make the notification max priority.
builder.setPriority(Notification.PRIORITY_MAX);
// Make head-up notification.
builder.setFullScreenIntent(pendingIntent, true);


// Build the notification.
Notification notification = builder.build();

// Start foreground service.
startForeground(1, notification);
}
private void stopForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");

// Stop foreground service and remove the notification.
stopForeground(true);

// Stop the foreground service.
stopSelf();
}









share|improve this question




















  • 1




    This answer should help you: stackoverflow.com/a/53081689/4255978
    – HedeH
    Nov 21 '18 at 10:05










  • @HedeH what is this Channel name?Where do i know what is the name?
    – Alex Kolydas
    Nov 21 '18 at 10:09






  • 1




    It doesn't matter, It's just for Android to manage the notifications. You can read about notification channels if you want to lear more: developer.android.com/training/notify-user/channels
    – HedeH
    Nov 21 '18 at 10:19










  • @HedeH Thank you very much!Works fine!Just one more question.I want to implement my own title,text and image and i want also when the user presses on the notification to open the app.Now if the user presses the app it opens settings.Any ideas?
    – Alex Kolydas
    Nov 21 '18 at 11:16






  • 1




    It opens settings because you didn't specify a PendingIntent, title etc.. So the system is creating the notification for you. You need to add stuff to the Notification.Builder. Look here: developer.android.com/training/notify-user/build-notification
    – HedeH
    Nov 21 '18 at 11:22


















3














I have a foreground service that runs on my phone(Android 7.0) but it doesn't run on a friends phone(Android 8.0).I also test it on android studio emulators and i have the same problem on version 8.0.I've implemented the if-else check to see if it's Android 8.0 so i can call startForegroundService() and still nothing.



Here is how i call it:



 //Start foreground service
Intent intent = new Intent(this, MyForeGroundService.class);
intent.setAction(MyForeGroundService.ACTION_START_FOREGROUND_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
}else{
startService(intent);
}


And here is my foreground service:



public class MyForeGroundService extends Service implements LocationListener {

public MyForeGroundService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();
wakeLock();
}

@SuppressLint("InvalidWakeLockTag")
public void wakeLock() {
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
assert pm != null;
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "partialwl");
wl.acquire();
}

public void stopWakeLock() {
wl.release();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();

switch (action) {
case ACTION_START_FOREGROUND_SERVICE:
wakeLock();
startForegroundService();
break;
case ACTION_STOP_FOREGROUND_SERVICE:
stopWakeLock();
stopForegroundService();
break;
case ACTION_PLAY:
Intent openMap= new Intent(this,Map.class);
startActivity(openMap);
break;
case ACTION_PAUSE:
break;
}
}
return START_STICKY;
}

/* Used to build and start foreground service. */
private void startForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Start foreground service.");

// Create notification default intent.
Intent intent = new Intent(this,Map.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

// Create notification builder.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

//Go back to Map activity if user press at the notification
builder.setContentIntent(pendingIntent)
.setContentTitle("gEKOning...")
.setContentText("Tap to open gEKOn app");


builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.mipmap.ic_launcher);
Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(largeIconBitmap);
// Make the notification max priority.
builder.setPriority(Notification.PRIORITY_MAX);
// Make head-up notification.
builder.setFullScreenIntent(pendingIntent, true);


// Build the notification.
Notification notification = builder.build();

// Start foreground service.
startForeground(1, notification);
}
private void stopForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");

// Stop foreground service and remove the notification.
stopForeground(true);

// Stop the foreground service.
stopSelf();
}









share|improve this question




















  • 1




    This answer should help you: stackoverflow.com/a/53081689/4255978
    – HedeH
    Nov 21 '18 at 10:05










  • @HedeH what is this Channel name?Where do i know what is the name?
    – Alex Kolydas
    Nov 21 '18 at 10:09






  • 1




    It doesn't matter, It's just for Android to manage the notifications. You can read about notification channels if you want to lear more: developer.android.com/training/notify-user/channels
    – HedeH
    Nov 21 '18 at 10:19










  • @HedeH Thank you very much!Works fine!Just one more question.I want to implement my own title,text and image and i want also when the user presses on the notification to open the app.Now if the user presses the app it opens settings.Any ideas?
    – Alex Kolydas
    Nov 21 '18 at 11:16






  • 1




    It opens settings because you didn't specify a PendingIntent, title etc.. So the system is creating the notification for you. You need to add stuff to the Notification.Builder. Look here: developer.android.com/training/notify-user/build-notification
    – HedeH
    Nov 21 '18 at 11:22
















3












3








3







I have a foreground service that runs on my phone(Android 7.0) but it doesn't run on a friends phone(Android 8.0).I also test it on android studio emulators and i have the same problem on version 8.0.I've implemented the if-else check to see if it's Android 8.0 so i can call startForegroundService() and still nothing.



Here is how i call it:



 //Start foreground service
Intent intent = new Intent(this, MyForeGroundService.class);
intent.setAction(MyForeGroundService.ACTION_START_FOREGROUND_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
}else{
startService(intent);
}


And here is my foreground service:



public class MyForeGroundService extends Service implements LocationListener {

public MyForeGroundService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();
wakeLock();
}

@SuppressLint("InvalidWakeLockTag")
public void wakeLock() {
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
assert pm != null;
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "partialwl");
wl.acquire();
}

public void stopWakeLock() {
wl.release();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();

switch (action) {
case ACTION_START_FOREGROUND_SERVICE:
wakeLock();
startForegroundService();
break;
case ACTION_STOP_FOREGROUND_SERVICE:
stopWakeLock();
stopForegroundService();
break;
case ACTION_PLAY:
Intent openMap= new Intent(this,Map.class);
startActivity(openMap);
break;
case ACTION_PAUSE:
break;
}
}
return START_STICKY;
}

/* Used to build and start foreground service. */
private void startForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Start foreground service.");

// Create notification default intent.
Intent intent = new Intent(this,Map.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

// Create notification builder.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

//Go back to Map activity if user press at the notification
builder.setContentIntent(pendingIntent)
.setContentTitle("gEKOning...")
.setContentText("Tap to open gEKOn app");


builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.mipmap.ic_launcher);
Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(largeIconBitmap);
// Make the notification max priority.
builder.setPriority(Notification.PRIORITY_MAX);
// Make head-up notification.
builder.setFullScreenIntent(pendingIntent, true);


// Build the notification.
Notification notification = builder.build();

// Start foreground service.
startForeground(1, notification);
}
private void stopForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");

// Stop foreground service and remove the notification.
stopForeground(true);

// Stop the foreground service.
stopSelf();
}









share|improve this question















I have a foreground service that runs on my phone(Android 7.0) but it doesn't run on a friends phone(Android 8.0).I also test it on android studio emulators and i have the same problem on version 8.0.I've implemented the if-else check to see if it's Android 8.0 so i can call startForegroundService() and still nothing.



Here is how i call it:



 //Start foreground service
Intent intent = new Intent(this, MyForeGroundService.class);
intent.setAction(MyForeGroundService.ACTION_START_FOREGROUND_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
}else{
startService(intent);
}


And here is my foreground service:



public class MyForeGroundService extends Service implements LocationListener {

public MyForeGroundService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();
wakeLock();
}

@SuppressLint("InvalidWakeLockTag")
public void wakeLock() {
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
assert pm != null;
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "partialwl");
wl.acquire();
}

public void stopWakeLock() {
wl.release();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();

switch (action) {
case ACTION_START_FOREGROUND_SERVICE:
wakeLock();
startForegroundService();
break;
case ACTION_STOP_FOREGROUND_SERVICE:
stopWakeLock();
stopForegroundService();
break;
case ACTION_PLAY:
Intent openMap= new Intent(this,Map.class);
startActivity(openMap);
break;
case ACTION_PAUSE:
break;
}
}
return START_STICKY;
}

/* Used to build and start foreground service. */
private void startForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Start foreground service.");

// Create notification default intent.
Intent intent = new Intent(this,Map.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

// Create notification builder.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

//Go back to Map activity if user press at the notification
builder.setContentIntent(pendingIntent)
.setContentTitle("gEKOning...")
.setContentText("Tap to open gEKOn app");


builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.mipmap.ic_launcher);
Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(largeIconBitmap);
// Make the notification max priority.
builder.setPriority(Notification.PRIORITY_MAX);
// Make head-up notification.
builder.setFullScreenIntent(pendingIntent, true);


// Build the notification.
Notification notification = builder.build();

// Start foreground service.
startForeground(1, notification);
}
private void stopForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");

// Stop foreground service and remove the notification.
stopForeground(true);

// Stop the foreground service.
stopSelf();
}






android android-8.0-oreo foreground-service






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:48









Fantômas

32.4k156388




32.4k156388










asked Nov 21 '18 at 9:34









Alex KolydasAlex Kolydas

329114




329114








  • 1




    This answer should help you: stackoverflow.com/a/53081689/4255978
    – HedeH
    Nov 21 '18 at 10:05










  • @HedeH what is this Channel name?Where do i know what is the name?
    – Alex Kolydas
    Nov 21 '18 at 10:09






  • 1




    It doesn't matter, It's just for Android to manage the notifications. You can read about notification channels if you want to lear more: developer.android.com/training/notify-user/channels
    – HedeH
    Nov 21 '18 at 10:19










  • @HedeH Thank you very much!Works fine!Just one more question.I want to implement my own title,text and image and i want also when the user presses on the notification to open the app.Now if the user presses the app it opens settings.Any ideas?
    – Alex Kolydas
    Nov 21 '18 at 11:16






  • 1




    It opens settings because you didn't specify a PendingIntent, title etc.. So the system is creating the notification for you. You need to add stuff to the Notification.Builder. Look here: developer.android.com/training/notify-user/build-notification
    – HedeH
    Nov 21 '18 at 11:22
















  • 1




    This answer should help you: stackoverflow.com/a/53081689/4255978
    – HedeH
    Nov 21 '18 at 10:05










  • @HedeH what is this Channel name?Where do i know what is the name?
    – Alex Kolydas
    Nov 21 '18 at 10:09






  • 1




    It doesn't matter, It's just for Android to manage the notifications. You can read about notification channels if you want to lear more: developer.android.com/training/notify-user/channels
    – HedeH
    Nov 21 '18 at 10:19










  • @HedeH Thank you very much!Works fine!Just one more question.I want to implement my own title,text and image and i want also when the user presses on the notification to open the app.Now if the user presses the app it opens settings.Any ideas?
    – Alex Kolydas
    Nov 21 '18 at 11:16






  • 1




    It opens settings because you didn't specify a PendingIntent, title etc.. So the system is creating the notification for you. You need to add stuff to the Notification.Builder. Look here: developer.android.com/training/notify-user/build-notification
    – HedeH
    Nov 21 '18 at 11:22










1




1




This answer should help you: stackoverflow.com/a/53081689/4255978
– HedeH
Nov 21 '18 at 10:05




This answer should help you: stackoverflow.com/a/53081689/4255978
– HedeH
Nov 21 '18 at 10:05












@HedeH what is this Channel name?Where do i know what is the name?
– Alex Kolydas
Nov 21 '18 at 10:09




@HedeH what is this Channel name?Where do i know what is the name?
– Alex Kolydas
Nov 21 '18 at 10:09




1




1




It doesn't matter, It's just for Android to manage the notifications. You can read about notification channels if you want to lear more: developer.android.com/training/notify-user/channels
– HedeH
Nov 21 '18 at 10:19




It doesn't matter, It's just for Android to manage the notifications. You can read about notification channels if you want to lear more: developer.android.com/training/notify-user/channels
– HedeH
Nov 21 '18 at 10:19












@HedeH Thank you very much!Works fine!Just one more question.I want to implement my own title,text and image and i want also when the user presses on the notification to open the app.Now if the user presses the app it opens settings.Any ideas?
– Alex Kolydas
Nov 21 '18 at 11:16




@HedeH Thank you very much!Works fine!Just one more question.I want to implement my own title,text and image and i want also when the user presses on the notification to open the app.Now if the user presses the app it opens settings.Any ideas?
– Alex Kolydas
Nov 21 '18 at 11:16




1




1




It opens settings because you didn't specify a PendingIntent, title etc.. So the system is creating the notification for you. You need to add stuff to the Notification.Builder. Look here: developer.android.com/training/notify-user/build-notification
– HedeH
Nov 21 '18 at 11:22






It opens settings because you didn't specify a PendingIntent, title etc.. So the system is creating the notification for you. You need to add stuff to the Notification.Builder. Look here: developer.android.com/training/notify-user/build-notification
– HedeH
Nov 21 '18 at 11:22














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409020%2fforeground-service-doesnt-run-in-oreo%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409020%2fforeground-service-doesnt-run-in-oreo%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga