简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM