Android 13: determine if a Live Wallpaper is set

1k views Asked by At

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).

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.

Why? I searched on Google and on the Android Developer Documentation, but I did'n find anything...

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);
2

There are 2 answers

2
Robin On

Check the source code of 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

    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
0
Chunhui Deng On

There is a bug reported to Google. I'm not sure if Google is going to fix it. This is the workaround I've been using. LiveWallpaperService.isEngineRunning can tell your app is set as LiveWallpaper or not.

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
    }
}