How to broadcast an action to another application's BroadcastReceiver in Android?

81 Views Asked by At

As of android API 26 (android 8.0) android is limiting access to PendingIntent.getBroadcast which is mentioned here. So in order to send a broadcast at a specific time by using PendingIntent, I have to specify the receiver component's name in Intent.

But when I do not know that any app is hosting a receiver, is there any way sending broadcasts to another application's broadcast receiver?

1

There are 1 best solutions below

0
mohammad fakhraee On

I did a lot of test in order to create a broadcast which will target any application that receives the action. Here is the result:

You only will be able to target only one class of only one application!

You can easily target a class inside an application like this:

val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    Intent().apply {
        action = "SOME_ACTION_TO_BROADCAST"
        component = ComponentName("PACKAGE_NAME_OF_TARGET_APPLICATION", "TARGET_CLASS_PACKAGE.<ClassName>")

        val pi = PendingIntent.getBroadcast(context, 0, this, PendingIntent.FLAG_IMMUTABLE)
            
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, /* any time in future */ System.currentTimeMillis() + 5_000, pi)
}