设置动态壁纸不工作

设置动态壁纸不工作

本文介绍了设置动态壁纸不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过这个,但它已被迁移到android.stackexchange.com和关闭那里offtopic。
所以在这里,我们又来了:

我做了一个简单的动态壁纸。在preVIEW我可以看到它,但如果我尝试将其设置为我的动态壁纸的Andr​​oid保持旧墙纸那里。

任何想法?

AndroidManifest.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.localfotos
      安卓版code =1
      机器人:=的versionName1.0>
    <采用-SDK安卓的minSdkVersion =7/>  <使用特征的android:NAME =android.software.live_wallpaper/>    <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>        <服务机器人:名字=。MyWallpaperService
            机器人:标签=@字符串/ APP_NAME
            机器人:图标=@绘制/图标>            &所述;意图滤光器>
                <作用机器人:名字=android.service.wallpaper.WallpaperService/>
            &所述; /意图滤光器>
            <元数据机器人:名字=android.service.wallpaper
                机器人:资源=@ XML / livewallpaper/>        < /服务>    < /用途>
< /清单>

livewallpaper.xml

 <壁纸的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
/>

MyWallpaperService.java

 包com.localfotos;进口android.service.wallpaper.WallpaperService;
进口android.view.MotionEvent;
进口android.view.SurfaceHolder;公共类MyWallpaperService扩展WallpaperService {    @覆盖
    公共引擎onCreateEngine(){
        返回新MyEngine();
    }    @覆盖
    公共无效的onCreate(){
        super.onCreate();
    }    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
    }    公共类MyEngine扩展引擎{        私人MyWallpaperPainting画;        MyEngine(){
            SurfaceHolder支架= getSurfaceHolder();
            绘画=新MyWallpaperPainting(架,
                getApplicationContext());
        }        @覆盖
        公共无效的onCreate(SurfaceHolder surfaceHolder){
            super.onCreate(surfaceHolder);
            //注册监听器和回调这里
            setTouchEventsEnabled(真);
        }        @覆盖
        公共无效的onDestroy(){
            super.onDestroy();
            //删除监听器和回调这里
            painting.stopPainting();
        }        @覆盖
        公共无效onVisibilityChanged(布尔可见){
            如果(可见){
                //注册监听器和回调这里
                painting.resumePainting();
            }其他{
                //删除监听器和回调这里
                painting.pausePainting();
            }
        }        @覆盖
        公共无效onSurfaceChanged(SurfaceHolder架,INT格式,
                INT宽度,高度INT){
            super.onSurfaceChanged(保持器,格式,宽度,高度);
            painting.setSurfaceSize(宽度,高度);
        }        @覆盖
        公共无效onSurfaceCreated(SurfaceHolder持有人){
            super.onSurfaceCreated(保持器);
            //开始绘制
            painting.start();
        }        @覆盖
        公共无效onSurfaceDestroyed(SurfaceHolder持有人){
            super.onSurfaceDestroyed(保持器);
            布尔重试= TRUE;
            painting.stopPainting();
            而(重试){
                尝试{
                    painting.join();
                    重试= FALSE;
                }赶上(InterruptedException的E){}
            }
        }        @覆盖
        公共无效onOffsetsChanged(浮动xOffset,浮yOffset,
                浮XSTEP,浮yStep,诠释XPIXELS,诠释YPIXELS){
        }        @覆盖
        公共无效onTouchEvent(MotionEvent事件){
            super.onTouchEvent(事件);
            painting.doTouchEvent(事件);
        }    }}

MyWallpaperPainting.java

 包com.localfotos;进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.graphics.Path;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.SurfaceHolder;公共类MyWallpaperPainting继承Thread {
    私有静态最后弦乐TAG =MyWallpaperPainting;
    / **参考了查看和上下文* /
    私人SurfaceHolder surfaceHolder;
    私人上下文的背景下;    / **州* /
    私人布尔等;
    私人布尔运行;    / **尺寸* /
    私人诠释宽度;
    私人诠释高度;    / **时间跟踪* /
    私人长期previousTime;
    私人长期currentTime的;    公共MyWallpaperPainting(SurfaceHolder surfaceHolder,上下文的背景下){
        //保持上下文和面的基准
        //如果要膨胀是需要的上下文
        //从livewallpaper的apk一些资源
        this.surfaceHolder = surfaceHolder;
        this.context =背景;
        //不要移动,直到表面被创建并显示
        this.wait =真;
    }    / **
     *暂停动态壁纸动画
     * /
    公共无效pausePainting(){
        this.wait =真;
        同步(本){
            this.notify();
        }
    }    / **
     *简历动态壁纸动画
     * /
    公共无效resumePainting(){
        this.wait = FALSE;
        同步(本){
            this.notify();
        }
    }    / **
     *停止动态壁纸动画
     * /
    公共无效stopPainting(){
        this.run = FALSE;
        同步(本){
            this.notify();
        }
    }    @覆盖
    公共无效的run(){
        this.run = TRUE;
        帆布C = NULL;
        而(运行){
            尝试{
                C = this.surfaceHolder.lockCanvas(NULL);
                同步(this.surfaceHolder){
                    currentTime的= System.currentTimeMillis的();
                    updatePhysics();
                    doDraw(C);
                    previousTime = currentTime的;
                }
            } {最后
                如果(C!= NULL){
                    this.surfaceHolder.unlockCanvasAndPost(C);
                }
            }
            //暂停,如果没有必要动画
            同步(本){
                如果(等待){
                    尝试{
                        等待();
                    }赶上(例外五){}
                }
            }        同步(本){
            尝试{
                等待(1000);
            }赶上(InterruptedException的E){}
        }
      }
    }    / **
     *调用表面尺寸变化时
     * /
    公共无效setSurfaceSize(INT宽度,高度INT){
        this.width =宽度;
        this.height =高度;
        同步(本){
            this.notify();
        }
    }    / **
     *调用而屏幕被触摸
     * /
    公共无效doTouchEvent(MotionEvent事件){
        //此处处理该事件
        //如果有什么动画
        //再醒来
        this.wait = FALSE;
        同步(本){
            通知();
        }
    }    / **
     *做实际的绘图东西
     * /
    私人无效doDraw(帆布油画){
        / **
         *更新动画,精灵或什么的。
         *如果有什么动画设置等待
         *线程真正的属性
         * /          涂料mPaint =新的油漆();
          mPaint.setDither(真);
          mPaint.setColor(0xFFFFFF00);
          mPaint.setStyle(Paint.Style.STROKE);
          mPaint.setStrokeJoin(Paint.Join.ROUND);
          mPaint.setStrokeCap​​(Paint.Cap.ROUND);
          mPaint.setStrokeWidth(3);          路径path =新路径();
          path.lineTo(10,100);          canvas.drawColor(0xFFFFFF00);          Log.d(TAGdoDraw);    }    私人无效updatePhysics(){
        //如果没有被更新:
        // this.wait = TRUE;
    }}


解决方案

您清单缺少BIND_WALLPAPER许可。请参见SDK中立方的例子。

 安卓权限=android.permission.BIND_WALLPAPER>

I already asked this, but it was migrated to android.stackexchange.com and closed there as offtopic.So here we go again:

I made a simple live wallpaper. On the preview I can see it but if I try to set it as my live wallpaper Android keeps the old wallpaper there.

Any ideas?

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.localfotos"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

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

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

        <service android:name=".MyWallpaperService"
            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>

livewallpaper.xml

<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
/>

MyWallpaperService.java

package com.localfotos;

import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class MyWallpaperService extends WallpaperService {

    @Override
    public Engine onCreateEngine() {
        return new MyEngine();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public class MyEngine extends Engine {

        private MyWallpaperPainting painting;

        MyEngine() {
            SurfaceHolder holder = getSurfaceHolder();
            painting = new MyWallpaperPainting(holder,
                getApplicationContext());
        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            // register listeners and callbacks here
            setTouchEventsEnabled(true);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            // remove listeners and callbacks here
            painting.stopPainting();
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            if (visible) {
                // register listeners and callbacks here
                painting.resumePainting();
            } else {
                // remove listeners and callbacks here
                painting.pausePainting();
            }
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
            painting.setSurfaceSize(width, height);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            // start painting
            painting.start();
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            boolean retry = true;
            painting.stopPainting();
            while (retry) {
                try {
                    painting.join();
                    retry = false;
                } catch (InterruptedException e) {}
            }
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xStep, float yStep, int xPixels, int yPixels) {
        }

        @Override
        public void onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            painting.doTouchEvent(event);
        }

    }

}

MyWallpaperPainting.java

package com.localfotos;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class MyWallpaperPainting extends Thread {
    private static final String TAG = "MyWallpaperPainting";


    /** Reference to the View and the context */
    private SurfaceHolder surfaceHolder;
    private Context context;

    /** State */
    private boolean wait;
    private boolean run;

    /** Dimensions */
    private int width;
    private int height;

    /** Time tracking */
    private long previousTime;
    private long currentTime;

    public MyWallpaperPainting(SurfaceHolder surfaceHolder, Context context) {
        // keep a reference of the context and the surface
        // the context is needed if you want to inflate
        // some resources from your livewallpaper .apk
        this.surfaceHolder = surfaceHolder;
        this.context = context;
        // don't animate until surface is created and displayed
        this.wait = true;
    }

    /**
     * Pauses the live wallpaper animation
     */
    public void pausePainting() {
        this.wait = true;
        synchronized(this) {
            this.notify();
        }
    }

    /**
     * Resume the live wallpaper animation
     */
    public void resumePainting() {
        this.wait = false;
        synchronized(this) {
            this.notify();
        }
    }

    /**
     * Stop the live wallpaper animation
     */
    public void stopPainting() {
        this.run = false;
        synchronized(this) {
            this.notify();
        }
    }

    @Override
    public void run() {
        this.run = true;
        Canvas c = null;
        while (run) {
            try {
                c = this.surfaceHolder.lockCanvas(null);
                synchronized (this.surfaceHolder) {
                    currentTime = System.currentTimeMillis();
                    updatePhysics();
                    doDraw(c);
                    previousTime = currentTime;
                }
            } finally {
                if (c != null) {
                    this.surfaceHolder.unlockCanvasAndPost(c);
                }
            }
            // pause if no need to animate
            synchronized (this) {
                if (wait) {
                    try {
                        wait();
                    } catch (Exception e) {}
                }
            }

        synchronized (this){
            try{
                wait(1000);
            }catch (InterruptedException e){}
        }
      }
    }

    /**
     * Invoke when the surface dimension change
     */
    public void setSurfaceSize(int width, int height) {
        this.width = width;
        this.height = height;
        synchronized(this) {
            this.notify();
        }
    }

    /**
     * Invoke while the screen is touched
     */
    public void doTouchEvent(MotionEvent event) {
        // handle the event here
        // if there is something to animate
        // then wake up
        this.wait = false;
        synchronized(this) {
            notify();
        }
    }

    /**
     * Do the actual drawing stuff
     */
    private void doDraw(Canvas canvas) {
        /**
         * Update the animation, sprites or whatever.
         * If there is nothing to animate set the wait
         * attribute of the thread to true
         */

          Paint mPaint = new Paint();
          mPaint.setDither(true);
          mPaint.setColor(0xFFFFFF00);
          mPaint.setStyle(Paint.Style.STROKE);
          mPaint.setStrokeJoin(Paint.Join.ROUND);
          mPaint.setStrokeCap(Paint.Cap.ROUND);
          mPaint.setStrokeWidth(3);

          Path path = new Path();
          path.lineTo(10, 100);

          canvas.drawColor(0xFFFFFF00);

          Log.d(TAG, "doDraw");

    }

    private void updatePhysics() {
        // if nothing was updated :
        // this.wait = true;
    }

}
解决方案

Your manifest is missing the BIND_WALLPAPER permission. See the Cube example in the SDK.

android:permission="android.permission.BIND_WALLPAPER">

这篇关于设置动态壁纸不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 12:42