繁体   English   中英

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

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

我会检查我的动态壁纸应用程序是否设置为动态壁纸。

以下代码适用于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;
    }
}

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

为什么? 我在 Google 和 Android 开发人员文档上进行了搜索,但我没有找到任何东西......

编辑:我使用此代码设置了动态壁纸并且它可以工作,但是如果设置了动态壁纸,我无法以编程方式检查。

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

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

我们可以看到查询壁纸信息需要一些权限。

因此,请在您的 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