Search code examples
androidandroid-intentbroadcastreceiver

Start an activity in backround by using ACTION_MANAGE_OVERLAY_PERMISSION


I want to start an activity in backround (even if the app is killed) by using ACTION_MANAGE_OVERLAY_PERMISSION. It works only when the app is in the foreground. My code is below;

1.Manifest File

 <uses-permission android:name="android.permission.WAKE_LOCK" />

 <receiver
        android:name=".digitalclock.DigitalClockReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.SCREEN_OFF" />
        </intent-filter>
    </receiver>

2-BroadcastReceiver

 override fun onReceive(context: Context?, intent: Intent?) {

    if (intent?.action.equals(Intent.ACTION_SCREEN_OFF)) {

     
        if (Build.VERSION.SDK_INT >= 29) {
            val intent2 = Intent(context, DigitalClockActivity::class.java)
            intent2.putExtra("unlock_screen", true)
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context?.startActivity(intent2)
        } 

        }
    }

I also started a service in Broadcast and it receives always the intent-filter even if the app is killed. Service class also starts but activity not.


Solution

  • Try replacing

    val intent2 = Intent(context, DigitalClockActivity::class.java)
    

    with hardcoded package and class name

    val i = Intent();
    i.setClassName("com.test", "com.test.DigitalClockActivity")
    

    Change package and activity class name to your own!