1

I am using WallpaperService to set the live wallpaper on homescreen. I am able to set 10 pics from resources and rotate them,
based on static frequency using thread. I want user to give the option of selecting multiple frequency. I tried to pass the thread sleep duration with user selected frequency. But the thread which is running is neither stopping or updating the values.
Its updating wallpaper with the previous selected frequency. Im stuck with this app from 3 weeks at various levels. I have tried wallpaper manager and setting bitmap with services, alarm manager but all are stopping to set the wallpaper after certain period of time. So finally using Wallpaper service. Help to overcome from this issue.

class WallpaperEngine extends Engine {
    //frequency selected by user to update the wallpaper
    private final int Wallpaper_DURATION = utils.getFrequency;

    private int[] mImagesArray;
    private int mImagesArrayIndex = 0;
    private Thread mDrawWallpaper;

    public WallpaperEngine() {
        customWallpaperHelper = new CustomWallpaperHelper(getApplicationContext(), getResources());
        mImagesArray = new int[] {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};

        mDrawWallpaper = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        drawFrame();
                        incrementCounter();
                        Thread.sleep(Wallpaper_DURATION);
                    }
                } catch (Exception e) {
                    //
                }
            }
        });

        mDrawWallpaper.start();
    }

    private void incrementCounter() {
        mImagesArrayIndex++;

        if (mImagesArrayIndex >= mImagesArray.length) {
            mImagesArrayIndex = 0;
        }
    }

    private void drawFrame() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas canvas = null;

        try {
            canvas = holder.lockCanvas();

            if (canvas != null) {
                drawImage(canvas);
            }
        } finally {
            if (canvas != null) {
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }

    private void drawImage(Canvas canvas)
    {

        Bitmap image = BitmapFactory.decodeResource(getResources(),
                mImagesArray[mImagesArrayIndex]);
        Bitmap b=Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), true);
        canvas.drawBitmap(b, 0,0, null);
    }
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
LaJ
  • 45
  • 1
  • 7
  • Can we see how `utils.getFrequency` looks like? Nevertheless there was no place where the thread `mDrawWallpaper` was killed. So probably you're creating new instances anytime the user changes interval option. – Giddy Naya Aug 19 '19 at 13:34
  • Exactly that's what I want to know where to kill the thread. Utils.getFrequency is just a field which I have used to update the user selected interval from activity to Service. It's getting updated when user changes. – LaJ Aug 19 '19 at 17:31

0 Answers0