[英]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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.