1

A simple call to

Intent serviceIntent = new Intent(this, NotificationService.class); startService(serviceIntent);

is bringing the app to front if it's in the background, nothing happens if the app were closed. I want to avoid this behavior but, i do not know how to do it. It happened on android O and P. I've setted up the alarmManagerto wake up my app at specific time, then a pendingIntent starts an Activity (it does not show anything in the screen) and it checks the received action from the Intent to determine the action to be performed. I have 2 Services in my app and both of them bring app to front everytime they're waked up, this is annoying to user.

Gilian Marques
  • 417
  • 4
  • 16
  • restrictions has been added from android O thats the reason . – Jeeva Oct 21 '18 at 15:02
  • 1
    I don't think this is due to Android OS behavior, I think your own code is somehow starting the Activity at the time the Service starts. Consider dumping the result of `Activity.getIntent()` in `Activity.onCreate()`, and determine whether it is a launcher Intent, or your `alarmManager` Intent. – greeble31 Oct 21 '18 at 23:53
  • @greeble31 services behavior has been changed from Android O lucky enogh for the OP his app behaves differetly only on devices running android O and above – Jeeva Oct 22 '18 at 02:43
  • 1
    @Jeeva It did indeed change in Oreo, but not in a way that causes this problem. Oreo requires the app to be on the temporary whitelist; otherwise, Service creation fails and an exception gets thrown. What the OP seems to be describing is an Activity returning to the foreground, unprompted. – greeble31 Oct 22 '18 at 03:44

2 Answers2

0

I solved the problem by replacing PendingIntent.getActivity() by PendingIntent.getBroadcast() while creating the PendingIntentthat would be triggered by AlarmManager. Here is the code:

      private PendingIntent getNotifyIntent() {
         Intent notifyIntent = new Intent(mContext, MyBroadcastReceiver.class);
         return PendingIntent.getBroadcast(mContext, 0, notifyIntent, 0);
}

I already used this code before (PendingIntent.getActivity()) but i never saw the app comes to screen before. The activity I used was totally transparent and invisible, it did not put a layout on the screen. I used it because for some reason using Broadcast was not working. But now the application was opening, I presume it was because the transparent activity opened would check what should be done and closed, calling the activity before it to be displayed on the screen.

Resuming: the problem wasn't the service itself but, the way i was setting up the AlarmManager

Tank u All by the help!

Gilian Marques
  • 417
  • 4
  • 16
-1

Prior to Android 8.0, the usual way to create a foreground service was to create a background service, then promote that service to the foreground. With Android 8.0, there is a complication; the system doesn't allow a background app to create a background service. For this reason, Android 8.0 introduces the new method startForegroundService() to start a new service in the foreground. After the system has created the service, the app has five seconds to call the service's startForeground() method to show the new service's user-visible notification. If the app does not call startForeground() within the time limit, the system stops the service and declares the app to be ANR.

https://developer.android.com/about/versions/oreo/background
https://stackoverflow.com/a/47611385/6059583
Follow this link which shows how to show notiication to run service i background

Jeeva
  • 1,791
  • 2
  • 23
  • 42
  • My problem isn't that service does not start, it starts normally, the problem is that when service starts if the app is open in background it is bringed to front. I just need to run my foreground services without automatically show my app to user if it's open in background – Gilian Marques Oct 21 '18 at 15:15
  • then u need to show a notiication to show that ur service is running in background – Jeeva Oct 21 '18 at 15:18
  • I do, I start a service with Intent serviceIntent = new Intent(this, NotificationService.class); startService(serviceIntent); and inside the NotificationService i create a notification and call startForeground. Everything works correctly but if the app is already open in background it is showed to user, this is the problem, i don't want the app come from background and be showed to user just because i started a service. – Gilian Marques Oct 21 '18 at 15:22
  • -1 This appears to be instruction on how to start a foreground service, but does not address the OP's problem: That the app is resuming at unexpected times. – greeble31 Oct 21 '18 at 23:49
  • @greeble31 thats why i mentioned the link from android services which say services behaviour is completely changed – Jeeva Oct 22 '18 at 02:45