安卓动态壁纸服务onStop未被调用

时间:2015-09-24 05:54:28

标签: java android bitmap live-wallpaper

我创建了一个动态壁纸,它将用户选择的文件作为背景图像的GIF。

我遇到的问题是当我点击“设置壁纸”时。它开始我的壁纸服务,然后如果我回到我的动态壁纸,然后点击“设置壁纸”#39;再一次,它似乎并没有关闭以前的服务,但也只是运行另一个服务。这意味着每次我点击设置壁纸'来自用户SD卡的图像被读入Bitmap变量

我的onDestroy()方法使所有Bitmap引用无效并且执行System.gc(),但是在这种情况下,当在它上面设置相同的壁纸时,服务似乎没有被销毁。

这是我的wallpersetter类

@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.button) {
        Intent intent2 = new Intent(DisplayImage.this, GifWallpaper.class);
        intent2.putExtra("pos", imageUrl);
        stopService(intent2);
        startService(intent2);


        Intent intent = new Intent(
                WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);

        if (Build.VERSION.SDK_INT > 15) {

            String pkg = GifWallpaper.class.getPackage().getName();
            String cls = GifWallpaper.class.getCanonicalName();
            intent.putExtra(
                    WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                    new ComponentName(pkg, cls));
        } else {
            intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
        }


        startActivityForResult(intent, 0);
    } else if (v.getId() == R.id.button2) {
        this.finish();
    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 0)
        this.finish();

    super.onActivityResult(requestCode, resultCode, data);

}

这是我的壁纸引擎类

 public WallPaperEngine() throws IOException {

        InputStream is = getResources().openRawResource(imag);

        if (is != null) {

            try {
                liveMovie = Movie.decodeStream(is);
                duration = liveMovie.duration();

            } finally {
                is.close();
            }
        } else {
            throw new IOException("Unable to open R.raw.hand");
        }
        mWhen = -1;
        runnable = new Runnable() {
            public void run() {
                nyan();
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        liveHandler.removeCallbacks(runnable);

    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);

        if (visible) {
            nyan();
        } else {
            liveHandler.removeCallbacks(runnable);
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
                                 int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);
        mScaleX = width / (1f * liveMovie.width());
        mScaleY = height / (1f * liveMovie.height());
        // mScaleX =  (width -liveMovie.width())/2;
        //  mScaleY=   (height - liveMovie.height())/2;

        nyan();
    }

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,
                                 float xOffsetStep, float yOffsetStep, int xPixelOffset,
                                 int yPixelOffset) {
        super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
                xPixelOffset, yPixelOffset);
        nyan();
    }

    void nyan() {
        tick();
        SurfaceHolder surfaceHolder = getSurfaceHolder();
        Canvas canvas = null;
        try {
            canvas = surfaceHolder.lockCanvas();
            if (canvas != null) {
                drawGif(canvas);
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
        liveHandler.removeCallbacks(runnable);
        if (isVisible()) {
            liveHandler.postDelayed(runnable, 1000L / 25L);
        }
    }

    void tick() {
        if (mWhen == -1L) {
            mWhen = 0;
            mStart = SystemClock.uptimeMillis();
        } else {
            long mDiff = SystemClock.uptimeMillis() - mStart;
            mWhen = (int) (mDiff % duration);
        }
    }

    void drawGif(Canvas canvas) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imag);
        cX = (canvas.getWidth() - bitmap.getWidth()) / 1.0f; //Width/2 gives the horizontal centre
        cY = (canvas.getHeight() - bitmap.getHeight()) / 8f;
        float cx, cy;
        cx = bitmap.getWidth();
        cy = bitmap.getHeight();

        canvas.save();
        if (cx > cy) {
            canvas.scale(mScaleX, mScaleY / 2);

            liveMovie.setTime(mWhen);
            liveMovie.draw(canvas, 0, cY);
        } else {
            canvas.scale(mScaleX, mScaleY);
            liveMovie.setTime(mWhen);
            liveMovie.draw(canvas, 0, 0);
        }

        canvas.restore();
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        super.onSurfaceDestroyed(holder);
   visible=false;
        liveHandler.removeCallbacks(runnable);
    }
}

2 个答案:

答案 0 :(得分:2)

WallpaperEngine无限期地在不同的线程上运行,所以你必须明确告诉它完成。

当您按下代码中的某个位置时,您应该能够访问正在运行的服务并执行:

myService.getThread().interrupt();

答案 1 :(得分:2)

首先检查是否

  

"服务完成后是否调用stopSelf()?"

因为一旦服务完成了你的启动它就应该调用它。读一读。看看this

我已经在我创建的动态壁纸上运行了一些测试,并且只有一次调用了WallpaperService onCreate(),无论我重新应用多少次,然后onDestory()也只被调用一次,当我应用不同的壁纸。所以,如果我正确理解你的问题,那么我会建议你这样做。但我不确定是什么导致它。您可以查看this code,您可以在Log.d()onCreate()中添加onDestroy()。让我知道它是否有帮助。