android - 全屏 Intent 不启动 Activity,但在 Android 10 上显示通知

标签 android android-intent broadcastreceiver android-pendingintent android-10.0

我尝试使用下一个代码启动广播接收器的 Activity

 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的通知 channel ,然后再次打开。稍后,当我调用NotificationManager.notify时,它会在通知抽屉中显示通知,但不会启动 Activity 。如果我删除该应用程序并重新安装,它会再次正常工作。这是我应该报告的 android 10 的错误吗?或者我可以对此做些什么?

最佳答案

在 Android 10 中,我们需要添加 USE_FULL_SCREEN_INTENT 权限

Permissions changes for fullscreen intents

  • 面向 Android 10 或更高版本并使用全屏 Intent 通知的应用必须在其应用的 list 文件中请求 USE_FULL_SCREEN_INTENT 权限。

  • 这是正常权限,因此系统会自动将其授予请求的应用。

确保您已在 list 文件中添加权限

示例代码

<?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>

关于android - 全屏 Intent 不启动 Activity,但在 Android 10 上显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57868564/

相关文章:

android - PhotoView(用于缩放)与 SwipeBack 不起作用

Android Permission Denial 启动Intent for Wallpaper Settings

android - 窃听电话号码 : I want my App to be in the application chooser

android - 将数据从 Activity 传递到 BroadcastReceiver

android - 禁用广播接收器有什么意义?

java - 下载管理器完成多次下载后如何执行任务

android - 网站(静态或动态)转换为 Android 应用程序

javascript - Android InAppBrowser _system 回调

javascript - 在 webview 中启用 Javascript 时,Android Webview 未检测到链接单击

android - 为 DownloadManager 的 BroadcastReceiver 设置附加功能