1

I am new to android development. I've been trying to open an app from a JobService that is scheduled from the onReceive() method of an implicit broadcast-receiver, declared in my manifest.

From some posts, I found that one can use the packageManager.getLaunchIntentForPackage() method and then use it to launch said package by starting a new activty with context.startActivity(), so I declared the function below:

fun openApp(packageName: String, context: Context){
            val startIntent: Intent? =
                context.packageManager.getLaunchIntentForPackage(packageName)
            startIntent?.addFlags(
                Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or
                        Intent.FLAG_ACTIVITY_NEW_TASK
            );
            context.startActivity(startIntent)
}

When used on the MainActivity file, this function works as expected, but when I try using it on my JobService, I noticed that it only works if my app has the Display Over Other Apps permission.

Is this to be expected? Is there any way to contourn this requirement? Is this because of the way I am passing the context in my openApp() function?

Thx in advance!

  • "Is this to be expected?" -- yes, as displaying activities from the background has been banned in recent versions of Android. Please raise a notification instead. – CommonsWare Jun 06 '22 at 15:29
  • So I shouldn't be able to automatically open an app from the background? This is a required feature in my case... :/ – Lucas Soares Jun 06 '22 at 15:37
  • "So I shouldn't be able to automatically open an app from the background?" -- correct. "This is a required feature in my case" -- interrupting the user by taking over the foreground has been considered to be a bad idea from the beginning. You do not know what the user is doing at the time, and the interruption could be a serious problem. For example, you might cause an accident if the user was relying on navigation at the time. – CommonsWare Jun 06 '22 at 15:46

1 Answers1

0

Thanks to CommonsWare's comments, I've found the documentation I needed:

Restrictions on starting activities from the background.

In my use case, I needed to cancel a call to a specific number and open my app instead, so I adapted my code to take advantage of the CallRedirectionService class, which is treated as an exception and therefore can start activities from the background.