android动态壁纸虽然已经安装但没有显示出来

时间:2012-06-14 18:30:06

标签: android live-wallpaper

我开始在Android上制作我的第一个动态壁纸主要是用这个: http://www.vogella.com/articles/AndroidLiveWallpaper/article.html。问题是壁纸没有出现在您可以选择壁纸的屏幕上。

我从eclipse启动了壁纸,卸载后重新安装了导出的壁纸,但它既不适用于我的智能手机也不适用于模拟器。

The Manifest:

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

    }
}

感谢任何可以提供帮助的人。

1 个答案:

答案 0 :(得分:1)

好吧,几个月后,我决定再次研究这个问题。我发现了错误......至少我是这么认为的。 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" />

注意:<wallpaper写成小写!我认为这就是问题所在。 :)