我正在使用全屏通知我的代码工作在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());
这是答案
现在我们需要在清单文件中添加<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
权限
针对Android Q或更高版本并使用全屏意图通知的应用必须在其应用的清单文件中请求USE_FULL_SCREEN_INTENT
权限。这是一个normal permission,因此系统会自动将其授予请求的应用程序
示例代码
<?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>