[英]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.