我开始在 android 上制作我的第一个动态壁纸,主要是使用这个: http ://www.vogella.com/articles/AndroidLiveWallpaper/article.html 。问题是壁纸没有显示在您可以选择壁纸的屏幕中。
我从 Eclipse 中启动了壁纸,卸载重新安装了导出的壁纸,但它既不适用于我的智能手机,也不适用于模拟器。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.zdev.wallpaper.cpubeat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.software.live_wallpaper" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service
android:name="de.zdev.wallpaper.CPUBeatService"
android:label="@string/app_name"
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>
“wallpaper.xml”(我认为问题不在这里):
<?xml version="1.0" encoding="utf-8"?>
<Wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="@drawable/ic_launcher"
android:description="@string/description" />
最后是我的“CPUBeatService.java”:
package de.zdev.wallpaper.cpubeat;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.service.wallpaper.WallpaperService.Engine;
import android.view.SurfaceHolder;
public class CPUBeatService extends WallpaperService {
@Override
public Engine onCreateEngine(){
return new CPUBeatEngine();
}
private class CPUBeatEngine extends Engine{
private final Handler handler = new Handler();
private boolean visible = true;
private int width, height;
private final Runnable drawRunnable = new Runnable(){
public void run(){
doDraw();
}
};
public CPUBeatEngine(){
handler.post(drawRunnable);
}
private void draw(Canvas c){
c.drawColor(Color.GREEN);
}
@Override
public void onVisibilityChanged(boolean v){
visible = v;
if(visible) handler.post(drawRunnable);
else handler.removeCallbacks(drawRunnable);
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder){
super.onSurfaceDestroyed(holder);
visible = false;
handler.removeCallbacks(drawRunnable);
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int w, int h){
super.onSurfaceChanged(holder, format, w, h);
width = w;
height = h;
}
private void doDraw(){
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
//do the real drawing now
draw(c);
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
handler.removeCallbacks(drawRunnable);
if (visible) {
handler.postDelayed(drawRunnable, 5000);
}
}
}
}
感谢任何能提供帮助的人。