androidbroadcastreceiverandroid-jobschedulerjobservice

Open an app from a JobService without "Display Over Other Apps" permission


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!


Solution

  • 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.