我在Android上学习动态壁纸。我注意到我的壁纸仍在运行,尽管我选择了另一个壁纸。要重现此行为,我将使用简单的动态壁纸。 清单是:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.wallpaper.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="23" />
<uses-feature android:name="android.software.live_wallpaper" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service
android:name=".DemoWallpaperService"
android:label="Demo Live Wallpaper"
android:permission="android.permission.BIND_WALLPAPER" >
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/wallpaper" />
</service>
</application>
</manifest>
服务代码是:
package org.wallpaper.test;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;
public class DemoWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
Log.i("DemoWallpaperService","DemoWallpaperService > onCreateEngine");
return new DemoWallpaperEngine();
}
@Override
public void onCreate() {
Log.i("DemoWallpaperService","DemoWallpaperService > onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
Log.i("DemoWallpaperService","DemoWallpaperService > onDestroy");
super.onDestroy();
}
private class DemoWallpaperEngine extends Engine {
private boolean mVisible = false;
private final Handler mHandler = new Handler();
private final Runnable mUpdateDisplay = new Runnable() {
@Override
public void run() {
draw();
}
};
private void draw() {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
Paint p = new Paint();
p.setTextSize(20);
p.setAntiAlias(true);
String text = "system time: " + Long.toString(System.currentTimeMillis());
float w = p.measureText(text, 0, text.length());
int offset = (int) w / 2;
int x = c.getWidth() / 2 - offset;
int y = c.getHeight() / 2;
p.setColor(Color.BLACK);
c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
p.setColor(Color.WHITE);
c.drawText(text, x, y, p);
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(mUpdateDisplay);
if (mVisible) {
mHandler.postDelayed(mUpdateDisplay, 100);
}
}
@Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
draw();
} else {
mHandler.removeCallbacks(mUpdateDisplay);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
draw();
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}
@Override
public void onDestroy() {
super.onDestroy();
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}
}
}
编译完成后,在设备上部署并选择它。
现在我选择另一个壁纸,所以我希望壁纸工艺会死掉。如果我去DDMS,我发现该过程仍在运行:
还有很多线程还活着:
这是正确的,还是壁纸没有正确关闭?
由于
答案 0 :(得分:0)
经过调查我发现,尽管服务调用onDestroy
方法,壁纸仍然存在。该过程放在缓存的进程中。如果您重新选择它,可以更快地恢复它。
当声明内存时,该过程将被终止。