设置为系统墙纸时如何更新活动背景?

How to update an activitys background when it's set to the systems wallpaper?

我有这段代码可以让我将当前设备壁纸设置为我的活动背景。

public void wallpaperBackground()
    {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
        Drawable wallpaperDrawable = wallpaperManager.getDrawable();

        View root = findViewById(R.id.home_view);
        root.setBackground(wallpaperDrawable);
    }

问题是,如果我通过设置更改壁纸,它不会在我的应用程序上更新,我只剩下以前的壁纸。我该如何解决这个问题?

其实很简单:

只需在 activity 中调用 onResume() 中的 wallpaperBackground() 方法即可。

这将在用户从另一个应用返回后 activity 恢复时自动应用正确的墙纸。