You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在基于Libgdx的Android动态壁纸中使用Oreo的WallpaperColors API

解决Android动态壁纸调用notifyColorsChanged()时的连接未建立警告

我明白你遇到的问题了——在onCreateApplication()里调用notifyColorsChanged()时出现Can't notify system because wallpaper connection was not established警告,这完全是时机不对导致的!

问题根源

在onCreateApplication()阶段,LibGDX正在初始化应用,但Android系统的WallpaperService Engine还没完成和系统的绑定连接。这时候调用notifyColorsChanged(),系统还没准备好接收壁纸颜色的通知,自然会抛出警告。

解决方案:找对调用时机

你需要把notifyColorsChanged()的调用移到壁纸Engine完全建立连接之后的时机,比如壁纸Surface创建完成时,或者Engine的onCreate()方法里。这里提供两种适配你LibGDX代码的方案:

方案1:在Surface创建完成时调用

修改你的MyLiveWallpaperListener类,重写onSurfaceCreated()方法,在这里调用颜色通知:

public class MyLiveWallpaperListener extends LiveWallpaperScreen implements AndroidWallpaperListener {
    public MyLiveWallpaperListener(Game game) {
        super(game);
    }

    // 保留你已有的offsetChange、previewStateChange方法...

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        super.onSurfaceCreated(gl, config);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            // 获取当前Service的Engine并通知颜色变化
            LiveWallpaperAndroid.this.getEngine().notifyColorsChanged();
            Log.d(TAG,"Notifying colors changed from onSurfaceCreated()");
        }
    }
}

方案2:重写Engine的onCreate()方法

在你的LiveWallpaperAndroid类里,重写onCreateEngine(),在Engine创建完成后立即调用通知:

public class LiveWallpaperAndroid extends AndroidLiveWallpaperService { 
    String TAG="LiveWallpaperAndroid";

    // 保留你已有的onCreateApplication方法...

    @Override
    public Engine onCreateEngine() {
        Engine engine = super.onCreateEngine();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            // 在Engine创建完成后通知颜色变化
            engine.notifyColorsChanged();
            Log.d(TAG,"Notifying colors changed from Engine onCreate()");
        }
        return engine;
    }

    // 保留你已有的MyLiveWallpaperListener内部类...
}

额外提醒

如果你的壁纸是动态生成内容的,确保在调用notifyColorsChanged()之前,已经完成了至少一帧的绘制——这样系统才能正确提取壁纸的颜色信息,否则可能无法触发系统UI的明暗模式切换。

内容的提问来源于stack exchange,提问作者Ajul Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:04:44