简体   繁体   English

Android 13:判断是否设置了动态壁纸

[英]Android 13: determine if a Live Wallpaper is set

I would to check if my Live Wallpaper App is set as Live Wallpaper.我会检查我的动态壁纸应用程序是否设置为动态壁纸。

The following code works on Android <= 12 , but not in Android 13 (sdk 33) .以下代码适用于Android <= 12 ,但不适用于Android 13 (sdk 33)

public static boolean isLiveWallpaper(Context context) {
    if (Service._handler == null) {
        return false;
    }
    WallpaperManager wpm = WallpaperManager.getInstance(context);
    WallpaperInfo info = wpm.getWallpaperInfo();
    try {
        return (info != null && info.getPackageName().equals(context.getPackageName()));
    } catch (Exception e) {
        return false;
    }
}

On Android 13 wpm.getWallpaperInfo() always return null .在 Android 13 wpm.getWallpaperInfo()上总是返回null

Why?为什么? I searched on Google and on the Android Developer Documentation, but I did'n find anything...我在 Google 和 Android 开发人员文档上进行了搜索,但我没有找到任何东西......

Edit: I set the live wallpaper with this code and it works, but I can't check programmatically if the live wallpaper is setted.编辑:我使用此代码设置了动态壁纸并且它可以工作,但是如果设置了动态壁纸,我无法以编程方式检查。

Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
        new ComponentName(context, Service.class));
context.startActivity(intent);

Check the source code of WallpaperManagerService.java查看WallpaperManagerService.java的源代码

    public WallpaperInfo getWallpaperInfo(int userId) {
    final boolean allow =
            hasPermission(READ_WALLPAPER_INTERNAL) || hasPermission(QUERY_ALL_PACKAGES);
    if (allow) {
        userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
                Binder.getCallingUid(), userId, false, true, "getWallpaperInfo", null);
        synchronized (mLock) {
            WallpaperData wallpaper = mWallpaperMap.get(userId);
            if (wallpaper != null && wallpaper.connection != null) {
                return wallpaper.connection.mInfo;
            }
        }
    }

    return null;
}

We can see some permissions are needed to query the wallpaper info.我们可以看到查询壁纸信息需要一些权限。

So, please add the following permission request in your AndroidManifest.xml因此,请在您的 AndroidManifest.xml 中添加以下权限请求

    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM