在 Android 8.1 中访问 WallpaperManager

Accessing WallpaperManager in Android 8.1

我正在构建一个启动器,需要访问用户当前的背景墙纸,但每次启动该应用程序时,我都会在日志中收到警告 W/WallpaperManager: No permission to access wallpaper, suppressing exception to avoid crashing legacy app.

这是我使用的代码:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Drawable wallpaperDrawable = wallpaperManager.getDrawable();

ImageView imageView = findViewById(R.id.background);
imageView.setImageDrawable(wallpaperDrawable);

此代码 returns 默认库存背景图片而不是我的实际背景图片。我在其他一些主流发射器(Evie、Launchair 等)上注意到了这个问题,但它似乎只发生在我最近更新的 Andorid 8.1 设备上。

进一步挖掘实际的 Android 源代码,我发现了这个:

if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.O) {
    Log.w(TAG, "No permission to access wallpaper, suppressing"
    + " exception to avoid crashing legacy app.");
} else {
    // Post-O apps really most sincerely need the permission.
    throw e;
}

但我似乎找不到对实际所需权限的引用。

感谢任何帮助!

对于 Api 27 (Android 8.1) 台设备,将 targetSdkVersion 更改为 27 并向清单添加 READ_EXTERNAL_STORAGE 权限可解决此问题。