Accessing WallpaperManager in Android 8.1

前提是你 提交于 2020-01-05 02:39:05

问题


I'm building a launcher and need to access the user's current background wallpaper but every time I launch the app I get the warning W/WallpaperManager: No permission to access wallpaper, suppressing exception to avoid crashing legacy app. in the logs.

Here is the code I'm using:

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

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

This code returns the default stock background image rather than my actual background image. I've noticed this problem on a few other mainstream launchers (Evie, Launchair, etc) but it only seems to happen on my recently-updated Andorid 8.1 device.

Digging further into the actual Android Source code, I've found this:

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;
}

But I can't seem to find a reference to the actual permission required.

Any help is appreciated!


回答1:


For Api 27 (Android 8.1) devices, changing the targetSdkVersion to 27 and adding the READ_EXTERNAL_STORAGE permission to the manifest fixes this.



来源:https://stackoverflow.com/questions/47995463/accessing-wallpapermanager-in-android-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!