【发布时间】:2023-12-14 08:13:01
【问题描述】:
我想通过使用 ACTION_MANAGE_OVERLAY_PERMISSION 在后台启动一个活动(即使应用程序被杀死)。它仅在应用程序处于前台时才有效。我的代码如下;
1.清单文件
<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-广播接收器
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)
}
}
}
我还在广播中启动了一项服务,即使应用程序被终止,它也始终接收到意图过滤器。服务类也开始了,但活动没有。
【问题讨论】:
-
你在哪里使用
ACTION_MANAGE_OVERLAY_PERMISSION在你的问题中写的代码? -
我有一个开关(默认为false),如果用户想切换为true,他必须授予权限,然后该属性将被启用,所以我在相关活动中进行这项工作。如果未启用该属性,则广播将永远不会在我的代码中工作。
-
所以你的广播接收器被触发但活动没有开始?
-
是的,确切地说,活动没有开始
标签: android android-intent broadcastreceiver