1

我正在编写一个简单的壁纸服务,类似于这个(Android 应用程序使用 Timer 定期更改壁纸)。

尽管我尝试绘制的图像很小(2kb png),但壁纸永远停留在“正在加载动态壁纸......”。

我的清单文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.TestWallpaper">
    <uses-sdk android:minSdkVersion="15"/>

        <uses-feature android:name="android.software.live_wallpaper" />

        <application android:icon="@drawable/icon"
                     android:label="@string/app_name"
                     android:permission="android.permission.BIND_WALLPAPER">

            <service android:name=".Wallpaper"
                     android:label="@string/app_name"
                     android:icon="@drawable/icon">

                <intent-filter>
                    <action android:name="android.service.wallpaper.WallpaperService" />
                </intent-filter>
                <meta-data android:name="android.service.wallpaper"
                           android:resource="@xml/livewallpaper" />

            </service>


        </application>

</manifest>

还有我的主类文件:

public class Wallpaper extends Service {

    Timer mytimer;
    int initialStart= 0;
    int interval=60000;
    Drawable drawable;
    WallpaperManager wpm;
    int prev=1;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("TestWallPaper=>", "print");
        mytimer=new Timer();
        wpm= WallpaperManager.getInstance(Wallpaper.this);
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mytimer.schedule(new TimerTask() {

            @Override
            public void run() {
                Log.i("TestWallPaper=>", "inside onStartCommand");
                if(prev==1){
                    Log.i("TestWallPaper=>", "prev==1");
                    drawable = getResources().getDrawable(R.drawable.blue_pin);
                    prev=2;
                }
                else if(prev==2){
                    Log.i("TestWallPaper=>", "prev==2");
                    drawable = getResources().getDrawable(R.drawable.red_pin);
                    prev=3;
                }
                else{
                    Log.i("TestWallPaper=>", "prev==3");
                    drawable = getResources().getDrawable(R.drawable.green_pin);
                    prev=1;
                }


                Bitmap wallpaper=((BitmapDrawable)drawable).getBitmap();

                try {
                    wpm.setBitmap(wallpaper);

                } catch (IOException e) {
                    Log.e("TestWallPaper=>", "inside onStartCommand");
                    e.printStackTrace();
                }

            }
        }, initialStart,interval);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        Log.i("TestWallPaper=>", "inside onBind");
        return null;
    }
}
4

0 回答 0