Java 桌面上的Android live墙纸服务未被调用
我已经创建了一个实时壁纸,它将用户选择的一个文件作为GIF作为背景图像 我遇到的问题是,当我单击“设置墙纸”时,它会开始我的墙纸服务,然后如果我返回到我的实时墙纸并再次单击“设置墙纸”,它似乎不会关闭以前的服务,而只是运行另一个服务。这意味着每次单击“设置墙纸”时,用户SD卡上的图像都会被读取到位图变量中 我的onDestroy()方法会使所有位图引用无效,并执行System.gc(),但是在这种情况下,在设置相同的墙纸时,服务似乎没有被破坏 这是我的wallpersetter课程Java 桌面上的Android live墙纸服务未被调用,java,android,bitmap,live-wallpaper,Java,Android,Bitmap,Live Wallpaper,我已经创建了一个实时壁纸,它将用户选择的一个文件作为GIF作为背景图像 我遇到的问题是,当我单击“设置墙纸”时,它会开始我的墙纸服务,然后如果我返回到我的实时墙纸并再次单击“设置墙纸”,它似乎不会关闭以前的服务,而只是运行另一个服务。这意味着每次单击“设置墙纸”时,用户SD卡上的图像都会被读取到位图变量中 我的onDestroy()方法会使所有位图引用无效,并执行System.gc(),但是在这种情况下,在设置相同的墙纸时,服务似乎没有被破坏 这是我的wallpersetter课程 @Overr
@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);
}
}
这是我的壁纸引擎类
@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);
}
}
引擎无限期地运行在不同的线程上,因此您必须明确地告诉它完成 按back键时,您应该能够访问正在运行的服务并执行以下操作:
myService.getThread().interrupt();
引擎无限期地运行在不同的线程上,因此您必须明确地告诉它完成 按back键时,您应该能够访问正在运行的服务并执行以下操作:
myService.getThread().interrupt();
首先检查一下
“服务是否在完成后调用stopSelf()?”
因为它应该在服务完成您启动它的目的后调用。读一读这个。看看吧我在我创建的一张实时壁纸上运行了一些测试,WallperService onCreate()只会被调用一次,无论我重新应用它多少次,当我应用不同的壁纸时,onDestory()也只会被调用一次。因此,如果我正确理解你的问题,那么我建议你这样做。但我不确定是什么原因造成的。您可以查看,您可以在
onCreate()
和onDestroy()中添加Log.d()
。让我知道它是否有用。首先检查
“服务是否在完成后调用stopSelf()?”
因为它应该在服务完成您启动它的目的后调用。读一读这个。看看吧
我在我创建的一张实时壁纸上运行了一些测试,WallperService onCreate()只会被调用一次,无论我重新应用它多少次,当我应用不同的壁纸时,onDestory()也只会被调用一次。因此,如果我正确理解你的问题,那么我建议你这样做。但我不确定是什么原因造成的。您可以查看,您可以在onCreate()
和onDestroy()中添加Log.d()
。如果有帮助,请告诉我。10-09 06:16:42.249 10627-10627/com.gurucharan.myapplication D/LIVE\u墙纸﹕ onCreate()10-09 06:16:45.659 10627-10627/com.gurucharan.myapplication D/LIVE_壁纸﹕ 10-09 06:18:21.519 10627-10627/com.gurucharan.myapplication D/LIVE_壁纸﹕ onDestroy()1 onDestroy()1 onDestroy正在被调用,但walpper未被更改10-09 06:16:42.249 10627-10627/com.gurucharan.myapplication D/LIVE_墙纸﹕ onCreate()10-09 06:16:45.659 10627-10627/com.gurucharan.myapplication D/LIVE_壁纸﹕ 10-09 06:18:21.519 10627-10627/com.gurucharan.myapplication D/LIVE_壁纸﹕ onDestroy()1 onDestroy()1 onDestroy正在被调用,但walpper没有被更改