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