簡體   English   中英

Android動態壁紙服務onStop沒有被調用

[英]Android live wallpaper service onStop is not getting called

我創建了一個動態壁紙,該壁紙將用戶選擇的文件作為GIF作為背景圖像。

我遇到的問題是,當我單擊“設置壁紙”時,它開始了我的壁紙服務,然后,如果我回到我的動態壁紙中,然后再次單擊“設置壁紙”,它似乎沒有關閉以前的服務,但是也會運行另一個。 這意味着每次我點擊“設置牆紙”時,來自用戶SD卡的圖像都會被讀取到位圖變量中

我的onDestroy()方法使所有Bitmap引用無效,並且執行System.gc(),但是在這種情況下,當在其上設置相同的牆紙時,似乎服務並未被破壞。

這是我的壁掛式排練班

@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);
    }
}

WallpaperEngine無限期地在其他線程上運行,因此您必須明確地告訴它完成。

按下代碼后,您應該可以在代碼中的某處訪問正在運行的服務並執行:

myService.getThread().interrupt();

首先檢查一下

“服務完成后是否正在調用stopSelf()?”

因為一旦服務完成您為其啟動的工作,就應該調用它。 閱讀此內容。 看看這個

我已經對創建的動態牆紙進行了一些測試,無論我重新應用幾次,WallpaperService的onCreate()都只會被調用一次,而當我應用其他牆紙時,onDestory()也只會被調用一次。 。 因此,如果我正確理解了您的問題,那么我會建議您。 但我不確定是什么原因造成的。 您可以查看此代碼 ,可以在onCreate()onDestroy()添加Log.d() onDestroy() 讓我知道是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM