REQUEST_IGNORE_BATTERY_OPTIMIZATIONS触发但未弹出提示

4
我将尝试在Android 9上使用推送通知启动应用时,通过ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS禁用电池优化。 在activity文件中(使用通知构建器):
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);
NotificationCompat.Builder...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getContext());
notificationManager.notify(notificationId, builder.build());
AndroidManifest.xml文件中:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

我能够显示推送通知,但是当你点击时,它只会消失。没有弹出“允许”的提示。我的设置是否有问题?


检查logcat。不要过滤它,否则可能会错过一些重要的信息。您还可以尝试更改“Intent”以打开一些不需要权限的其他应用程序,看看是否有效(仅为缩小问题范围)。 - David Wasser
这个权限仍然是Google推荐的吗? - IgorGanapolsky
1个回答

1

您的数据中缺少Uri。请参考文档

输入:Intent的数据URI必须指定要显示的应用程序包名称,使用"package"方案。即"package:com.my.app"。

请执行以下操作:

Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parseString("package:my.package.name");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,