9

我正在使用全屏通知我的代码可以在Oreo版本及以下版本中找到但是当我运行时android-Q我遇到了异常

Use of fullScreenIntent requires the USE_FULL_SCREEN_INTENT permission

我正在使用setFullScreenIntent()全屏通知

这是我的代码

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "123");

notificationBuilder.setAutoCancel(true)
            .setColor(ContextCompat.getColor(this, R.color.colorAccent))
            .setContentTitle(getString(R.string.app_name))
            .setContentText("Test")
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setFullScreenIntent(pendingIntent,true)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setAutoCancel(true);

mNotificationManager.notify(1000, notificationBuilder.build());
4

2 回答 2

14

这是答案

现在我们需要<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />在清单文件中添加权限

全屏意图的权限更改

针对 Android Q 或更高版本并使用全屏意图通知的应用程序必须USE_FULL_SCREEN_INTENT在其应用程序的清单文件中请求权限。这是一个正常的权限,所以系统会自动将它授予请求的应用程序

示例代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxx.xxx.xxx">

    <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>
于 2019-04-24T09:38:24.347 回答
0

设置全屏意图

意图启动而不是将通知发布到状态栏。仅用于需要用户立即注意的极高优先级通知,例如用户已明确设置为特定时间的来电或闹钟。如果此功能用于其他用途,请为用户提供关闭它并使用正常通知的选项,因为这可能会造成极大的破坏。

https://developer.android.com/reference/android/app/Notification.Builder

于 2020-01-28T22:23:57.370 回答