android - 全屏意图未启动活动,但确实在 android 10 上显示通知
问题描述
我尝试使用下一个代码为广播接收器启动活动
Intent i = new Intent(context, AlarmNotification.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) {
mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER,
"Whatever", NotificationManager.IMPORTANCE_HIGH));
}
mgr.notify(NOTIFY_ID, buildNormal(context, i).build());
}
private NotificationCompat.Builder buildNormal(Context context, Intent intent) {
NotificationCompat.Builder b=
new NotificationCompat.Builder(context, CHANNEL_WHATEVER);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(TEXT)
.setContentText(TEXT)
.setFullScreenIntent(buildPendingIntent(context, intent), true);
return(b);
}
private PendingIntent buildPendingIntent(Context context, Intent intent) {
return(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
一开始,一切正常。但是如果我进入应用设置,关闭CHANNEL_WHATEVER的通知通道,然后再打开。稍后当我调用 NotificationManager.notify 时,它会在通知抽屉中显示通知,但不会启动活动。如果我删除该应用程序并重新安装,它会再次正常工作。这是我应该报告的 android 10 的错误,还是我可以做些什么?
解决方案
在 Android 10 中,我们需要为USE_FULL_SCREEN_INTENT
全屏意图的权限更改
面向 Android 10 或更高版本并使用全屏意图通知的应用必须
USE_FULL_SCREEN_INTENT
在其应用的清单文件中请求权限。这是一个正常的权限,因此系统会自动将其授予请求的应用程序。
确保您已在清单文件中添加权限
示例代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nilu.demo">
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
推荐阅读
- django - 作为自动化测试的一部分,如何向本地 URL 发出请求?
- git - 无法通过“git add .”命令在 GIT 存储库中添加文件
- splunk - 是否有像 Toad 这样的 Splunk 工具允许在同一个编辑器中进行多个查询?
- c++ - C ++ Mixin - 初始化期间的动态绑定习语
- c - 使用两个处理器组的 C 线程处理
- javascript - 从 JavaScript 中的箭头函数返回数组
- android - 在 onBindViewHolder RecyclerView Firebase 适配器中检索用户 uid 详细信息 | FirebaseUI | 安卓,Java
- c# - 如何在 LINQ XML 中使用多个链接项进行查询
- javascript - 在同一行添加 2 个按钮
- javascript - 通过抓取 className,根据页面加载时指定的现有内容替换 DIV 内容