He creado mi primer fondo de pantalla de implementación en vivo en un hilo separado. Así que ahora tengo un WallpaperService y mi WallpaperPainter que hace el trabajo. El problema es que obtengo un IllegalArgumentException
en el método unlockCanvasAndPost
en algunos dispositivos (Samsung Note es el indicado). He leído todas las recomendaciones que pude encontrar pero no pude solucionar ese error. Parece que se llama al unlockCanvasAndPost
cuando se destruye la superficie, por lo que el lienzo no es válido. Aquí está lo esencial de código:IllegalArgumentException en unlockCanvasAndPost (android live wallpaper)
En el servicio de fondo de pantalla:
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
super.onSurfaceChanged(holder, format, width, height);
painting.setSurfaceSize(width, height);
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
painting.start();
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
painting.stopPainting();
while (retry) {
try {
painting.join();
retry = false;
} catch (InterruptedException e) { }
}
super.onSurfaceDestroyed(holder);
}
En el hilo de pintura:
public void stopPainting() {
this.run = false;
synchronized(this) {
this.notify();
}
}
public void run() {
this.run = true;
Canvas c = null;
while (run) {
try {
synchronized (this) {
Thread.sleep(50);
c = this.surfaceHolder.lockCanvas();
doDraw(c);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (c != null) {
this.surfaceHolder.unlockCanvasAndPost(c); // << -- HERE IS THE PROBLEM
}
}
// if pause...
synchronized (this) {
if (wait) {
try {
wait();
} catch (Exception e) { }
}
}
}
}
¿alguien puede darme alguna idea de lo que estoy haciendo mal? Soy nuevo para Java y Android.