2

当我调用该服务时,它没有打开动态壁纸选择器。

这是我选择动态壁纸的代码:

public class LiveWallpaper extends WallpaperService {
@Override
public Engine onCreateEngine() {
    return new WallpaperEngine();
}

private class WallpaperEngine extends Engine
{

    private final Handler handler = new Handler();
    private final Runnable drawRunner = new Runnable() {
        @Override
        public void run() {
            draw();
        }};

    public int width,height;
    private boolean visible = true;
    public Context context;
    private void draw()
   {
       SurfaceHolder holder=getSurfaceHolder();
       Canvas canvas=null;

     try  {
         canvas =holder.lockCanvas();
         canvas.save();
         if(canvas!=null)
         {
             drawGif(canvas);
         }
       }
     finally
     {
         if (canvas != null)
             holder.unlockCanvasAndPost(canvas);
     }
       handler.removeCallbacks(drawRunner);
       if (visible) {
           handler.postDelayed(drawRunner, 5000);
       }
   }

    @Override
    public void onVisibilityChanged(boolean visible) {
        this.visible = visible;
        if (visible) {
            handler.post(drawRunner);
        } else {
            handler.removeCallbacks(drawRunner);
        }
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    void drawGif(Canvas canvas)
    {
        canvas.save();
        WallpaperManager wallpaperManager=WallpaperManager.getInstance(context);
        Drawable drawable=wallpaperManager.getDrawable();
        //BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
       // Paint paint = new Paint();
       // paint.setAntiAlias(true);
      //  paint.setFilterBitmap(true);
       // paint.setDither(true);
        Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
         canvas.drawBitmap(bitmap,5,5,null);
        BitmapDrawable Bdrawable = new BitmapDrawable(getResources(), bitmap);

        HomeActivity.mainView.setBackground(Bdrawable);
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        super.onSurfaceDestroyed(holder);
        this.visible = false;
        handler.removeCallbacks(drawRunner);
    }


    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        this.width = width;
        this.height = height;
        super.onSurfaceChanged(holder, format, width, height);
        //holder=this.holder;

    }

}
}

这就是我调用服务的方式

Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
    intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
            new ComponentName(this, LiveWallpaper.class));

我的代码有问题吗?

提前致谢 :)

4

0 回答 0