0

When app is open then its working fine but when app is in background then BroadcastReceiver is called but activity intent not working

  class FakeCallReceiver : BroadcastReceiver() {
    
        override fun onReceive(context: Context, intent: Intent?) {
            
            LogUtils.d("===onReceive 1")
            setCustomSetting(context)
            LogUtils.d("===onReceive 2")
            val incomingCallActivity =  Intent(context.applicationContext, FakeCallActivity::class.java)
            incomingCallActivity.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(incomingCallActivity)
            LogUtils.d("===onReceive 3")
        }

        fun setCustomSetting(context: Context){
            val wakeLock: PowerManager.WakeLock =
                (context.getSystemService(Context.POWER_SERVICE) as PowerManager).run {
                    newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::MyWakelockTag").apply {
                        acquire()
                    }
                }
            val mKeyguard = (context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager).apply {
                newKeyguardLock("MyApp::Keyguard").disableKeyguard()
            }        
        }
    }

All logs are print there is no exception is occur but still FakeCallActivity is not called

MinSdkVersion = 24
TargetSdkVersion = 29

1. Have i made any mistake ?
2. Is there other way to open activity when app is in background ?

Sanjayrajsinh
  • 15,014
  • 7
  • 73
  • 78

1 Answers1

1

Starting Android 10 (API level 29), there are restrictions on opening an activity from the background. Check this - https://developer.android.com/guide/components/activities/background-starts.

You should try to avoid this or you can check these exceptions - https://developer.android.com/guide/components/activities/background-starts#exceptions. If any of the exceptions works for you then you can try doing that.

Deepanshu
  • 291
  • 1
  • 3
  • 8
  • how TrueCaller open dialog and how whatsapp open instant reply dialog – Sanjayrajsinh Jan 18 '21 at 13:51
  • @Sanjayrajsinh you can check the permissions which Truecaller takes. It takes some permissions which allows it to draw over other applications. And Whatsapp posts a notification instead of opening an activity. In Android, you can create notifications with reply capability. – Deepanshu Jan 18 '21 at 18:56