【问题标题】:Android 13: determine if a Live Wallpaper is setAndroid 13:确定是否设置了动态壁纸
【发布时间】:2022-11-06 10:58:37
【问题描述】:

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

以下代码适用于安卓 <= 12,但不在安卓 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);

【问题讨论】:

  • 该函数的文档声称它为静态图像返回 null,这可能是这种情况吗?
  • @PaulT。不,这不是,我设置了动态壁纸(我添加了一段代码)。并且相同的代码适用于以前的版本(<= 12)。
  • 那么对于 v13 的 Android 来说,这听起来像是一个问题?您的示例应该足以让他们重现。

标签: android live-wallpaper


【解决方案1】:

查看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"/>

【讨论】:

  • 谢谢,QUERY_ALL_PACKAGES 有效,但有一个警告:A &lt;queries&gt; declaration should generally be used instead of QUERY_ALL_PACKAGES; - https://developer.android.com/training/package-visibility/declaring 哪个是动态壁纸的正确查询?还是我必须使用“QUERY_ALL_PACKAGES”?
  • 如果您试图找出当前的动态壁纸组件是否在已知的包列表中,您可以在查询列表中提供该条目。否则,您无法提供您还不知道的姓名列表。但是,当您在 GooglePlay 上发布您的应用程序时,使用 QUERY_ALL_PACKAGES 权限需要额外的 Google 审核,请小心。
【解决方案2】:

向 Google 报告了 bug。我不确定谷歌是否会修复它。这是我一直在使用的解决方法。 LiveWallpaperService.isEngineRunning 可用于确定您的应用是否设置为 LiveWallpaper。

class LiveWallpaperService : WallpaperService() {
    ...
    
    class MyEngine : WallpaperService.Engine() {
        ...

        override fun onCreate(surfaceHolder: SurfaceHolder) {
            if (!isPreview) {
                isEngineRunning = true
            }
        }
    
        override fun onDestroy() {
            if (!isPreview) {
                isEngineRunning = false
            }
        }
    }

    companion object {
       var isEngineRunning = false
    }
}

【讨论】:

    猜你喜欢
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多