Sanjayrajsinh
Sanjayrajsinh

Reputation: 17098

Android : Activity not called from BroadcastReceiver when app is in background

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 ?

Upvotes: 0

Views: 380

Answers (1)

Deepanshu
Deepanshu

Reputation: 291

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.

Upvotes: 1

Related Questions