![](/img/trans.png)
[英]Send data from BroadcastReceiver to activity, while app is in background
[英]Android : Activity not called from BroadcastReceiver when app is in background
当应用程序打开时它工作正常但是当应用程序在后台时 BroadcastReceiver 被调用但活动意图不起作用
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()
}
}
}
打印所有日志,没有发生异常,但仍然没有调用FakeCallActivity
MinSdkVersion = 24
TargetSdkVersion = 29
1.我有错吗?
2. 后台有没有其他方法可以打开activity?
从 Android 10(API 级别 29)开始,从后台打开活动有限制。 检查这个 - https://developer.android.com/guide/components/activities/background-starts 。
您应该尽量避免这种情况,或者您可以检查这些异常 - https://developer.android.com/guide/components/activities/background-starts#exceptions 。 如果任何例外对您有用,那么您可以尝试这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.