I have this code:
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Handler;
import android.os.SystemClock;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by jreese on 7/12/2015.
*/
public class GIFWallpaperService extends WallpaperService{
//static final String TAG = "gifService";
static final String TAG="gifWallpaper";
static final Handler gifHandler = new Handler();
@Override
public void onCreate() {
super.onCreate();
}
@Override
public Engine onCreateEngine() {
try {
return new GifEngine();
} catch (IOException e) {
Log.w(TAG, "Error Creando Motor", e);
stopSelf();
return null;
}
}
class GifEngine extends Engine {
private final Movie gif;
private final int duration;
private final Runnable runnable;
float scaleX;
float scaleY;
int when;
long start;
/////
Globals globals= new Globals();
private int idImagen;
private Integer []imagenes= {R.raw.uno,R.raw.dos,R.raw.tres,R.raw.cuatro,R.raw.cinco,
R.raw.seis,R.raw.siete,R.raw.ocho,R.raw.nueve,R.raw.diez,R.raw.once,R.raw.doce,
R.raw.trece,R.raw.catorce,R.raw.quince,R.raw.dieciseis,R.raw.diecisiete,
R.raw.dieciocho,R.raw.diecinueve,R.raw.veinte,R.raw.veintiuno,R.raw.veintidos,
R.raw.veintitres,R.raw.veinticuatro,R.raw.veinticinco,R.raw.veintiseis,
R.raw.veintisiete,R.raw.veintiocho,R.raw.veintinueve,R.raw.treinta};
GifEngine() throws IOException {
idImagen=globals.getIdImagen();
InputStream is = getResources().openRawResource(imagenes[idImagen]);
if (is == null) {
throw new IOException("No se puede cargar la imagen");
}
try {
gif = Movie.decodeStream(is);
duration = gif.duration();
} finally {
is.close();
}
when = -1;
runnable = new Runnable() {
@Override
public void run() {
animateGif();
}
};
}
@Override
public void onDestroy() {
super.onDestroy();
gifHandler.removeCallbacks(runnable);
}
@Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (visible) {
animateGif();
} else {
gifHandler.removeCallbacks(runnable);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
scaleX = width / (1f * gif.width());
scaleY = height / (1f * gif.height());
animateGif();
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xOffsetStep, float yOffsetStep,
int xPixelOffset, int yPixelOffset) {
super.onOffsetsChanged(
xOffset, yOffset,
xOffsetStep, yOffsetStep,
xPixelOffset, yPixelOffset);
animateGif();
}
void animateGif() {
tick();
SurfaceHolder surfaceHolder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
gifCanvas(canvas);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
gifHandler.removeCallbacks(runnable);
if (isVisible()) {
gifHandler.postDelayed(runnable, 1000L/25L);
}
}
void tick() {
if (when == -1L) {
when = 0;
start = SystemClock.uptimeMillis();
} else {
long diff = SystemClock.uptimeMillis() - start;
when = (int) (diff % duration);
}
}
void gifCanvas(Canvas canvas) {
canvas.save();
canvas.scale(scaleX, scaleY);
gif.setTime(when);
gif.draw(canvas, 0, 0);
canvas.restore();
}
}
}
I have a startup activity which sends me an index number to choose the wallpaper that will show as wallpaper, well when running the application in the emulator is chosen and normally set the wallpaper but if I want to change I open the application again and I choose and supposedly apply the new gif but it keeps the one that does not change anything.
What am I missing?