我会检查我的生活壁纸应用程序是否设置为现场墙纸。
以下代码在Android 12上工作,但在Android13 (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;
}
}在Android13上,wpm.getWallpaperInfo()总是返回null。
为什么?我搜索了谷歌和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);发布于 2022-09-16 15:21:01
检查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"/>发布于 2022-11-06 02:58:37
有一个错误向谷歌报告。我不确定谷歌是否会修复它。这就是我一直在用的解决办法。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
}
}https://stackoverflow.com/questions/73521644
复制相似问题