简体   繁体   English

Android:如何确定是否设置了动态壁纸

[英]Android: how to determine if a Live Wallpaper is set or not

I would like to know if my live wallpaper has been set or not, this is because I have a button "set wallpaper" that I would like to enable only if my wallpaper hasn't already been set. 我想知道是否设置了我的动态壁纸,这是因为我有一个“设置壁纸”按钮,只有在尚未设置我的壁纸时,我才希望启用它。

Thaks to all 向所有人致敬

Thanks to ImZaat's answer I found the solution, ImZaat's code is not working for me, as I would like to know if my wallpaper is running, but not from the wallpaper engine itself, from one other activity instead (it's an Activity used for setting some preferences about the wallpaper, so it's inside the same packege). 多亏了ImZaat的回答,我找到了解决方案,ImZaat的代码对我不起作用,因为我想知道我的墙纸是否正在运行,而不是来自墙纸引擎本身,而是来自另一个活动(这是一个用于设置一些活动的活动关于壁纸的偏好设置,因此它位于同一包内)。

This is what I did and it's working fine (the code is inside the onCreate() method in the Activity: 这是我所做的,并且工作正常(代码位于Activity中的onCreate()方法内部:

    WallpaperManager wpm = WallpaperManager.getInstance(this);
    WallpaperInfo info = wpm.getWallpaperInfo();

    if (info != null && info.getPackageName().equals(this.getPackageName())) {
        Log.d(TAG, "We're already running");
    } else {
        Log.d(TAG, "We're not running");
    }

Would you settle for just checking if your wallpaper is already set? 您愿意只检查墙纸是否已设置好吗?

In your implementation of WallpaperService#onCreateEngine() you could do: 在实现WallpaperService#onCreateEngine()时,您可以执行以下操作:

WallpaperManager wpm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
WallpaperInfo info = wpm.getWallpaperInfo();
if (info != null && info.getComponent().equals(new ComponentName(this, getClass()))) {
    Log.d(TAG, "We're already running");
    // Still might be a preview, but the user is already running your LWP.
} else {
    Log.d(TAG, "We're not running, this should be a preview");
    // Should definitely be a preview.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM