动态壁纸从活动呼叫似乎不起作用

时间:2015-03-09 10:43:57

标签: android

我试图通过按钮点击活动调用Livewallpaperservice类,但似乎它没有调用我搜索到处但是找不到任何建议请帮助我,这是我的代码,我想知道这是这个可能的,

public class MainActivity extends ActionBarActivity {
    private ImageView view, img;
    private AnimationDrawable Anim;
    private RelativeLayout container_relativelayout;
    int REQUEST_SET_LIVE_WALLPAPER = 200;
    private int sdk;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        sdk = android.os.Build.VERSION.SDK_INT;

        secondAnimationstart(R.drawable.candledim, R.drawable.candledim1,
                R.drawable.candledim2);

        img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                ComponentName component = new                           ComponentName(getPackageName(),
                        getPackageName() + ".LiveWallpaperService");
                Intent intent = new Intent(
                        WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
                intent.putExtra(
                        WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                        component);
                startActivityForResult(intent, REQUEST_SET_LIVE_WALLPAPER);

            }
        });

    private void init() {
        container_relativelayout = (RelativeLayout)
        findViewById(R.id.container_relativelayout);
        view = (ImageView) findViewById(R.id.view);
        img = (ImageView) findViewById(R.id.img);
        view.setId(1);

    }





public class LiveWallpaperService extends WallpaperService {

    static final String TAG = "THEROCK_WALLPAPER";
    static final Handler liveHandler = new Handler();

    @Override
    public Engine onCreateEngine() {
        try {
            return new WallPaperEngine();
        } catch (IOException e) {
            Log.w(TAG, "Error creating WallPaperEngine", e);
            stopSelf();
            return null;
        }
    }

    class WallPaperEngine extends Engine {

        private Movie liveMovie;
        private int duration;
        private Runnable runnable;
        float mScaleX;
        float mScaleY;
        int mWhen;
        long mStart;

        public WallPaperEngine() throws IOException {

            InputStream is = getResources().openRawResource(R.raw.candle);
            if (is != null) {

                try {
                    liveMovie = Movie.decodeStream(is);
                    duration = liveMovie.duration();

                } finally {
                    is.close();
                }
            } else {
                throw new IOException("Unable to open R.raw.hand");
            }
            mWhen = -1;
            runnable = new Runnable() {
                public void run() {
                    nyan();
                }
            };
        }

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

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            if (visible) {
                nyan();
            } else {
                liveHandler.removeCallbacks(runnable);
            }
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
            mScaleX = width / (1f * liveMovie.width());
            mScaleY = height / (1f * liveMovie.height());
            nyan();
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xOffsetStep, float yOffsetStep, int xPixelOffset,
                int yPixelOffset) {
            super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
                    xPixelOffset, yPixelOffset);
            nyan();
        }

        void nyan() {
            tick();
            SurfaceHolder surfaceHolder = getSurfaceHolder();
            Canvas canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas();
                if (canvas != null) {
                    drawGif(canvas);
                }
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            liveHandler.removeCallbacks(runnable);
            if (isVisible()) {
                liveHandler.postDelayed(runnable, 1000L / 25L);
            }
        }

        void tick() {
            if (mWhen == -1L) {
                mWhen = 0;
                mStart = SystemClock.uptimeMillis();
            } else {
                long mDiff = SystemClock.uptimeMillis() - mStart;
                mWhen = (int) (mDiff % duration);
            }
        }

        void drawGif(Canvas canvas) {
            canvas.save();
            canvas.scale(mScaleX, mScaleY);
            liveMovie.setTime(mWhen);
            liveMovie.draw(canvas, 0, 0);
            canvas.restore();
        }
    }
}

这是mainfest.xml

<manifest 
    <uses-feature
        android:name="android.software.live_wallpaper"
        android:required="true" >
    </uses-feature>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="MainActivity"
            android:hardwareAccelerated="false"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <service
                android:name="LiveWallpaperService"
                android:enabled="true"
                android:label="Wallpaper Example "
                android:permission="android.permission.BIND_WALLPAPER" >
                <intent-filter>     
        <action android:name="android.service.wallpaperWallpaperService" >
                    </action>
                </intent-filter>
                <meta-data
                    android:name="android.service.wallpaper"
                    android:resource="@xml/mywallpaper" >
                </meta-data>
            </service>
            <activity
                android:name="com.candlelight.MyWallpaper"
                android:configChanges="keyboardHidden|orientation"
                android:exported="true" />
            <activity
                android:name="com.candlelight.UserSetting"
                android:configChanges="keyboardHidden|orientation"
                android:exported="true" />
        </activity>
    </application>
</manifest>

xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container_relativelayout"
        tools:context="com.candlelight.MainActivity" >

    <ImageView
        android:id="@+id/view"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"   
        android:layout_marginBottom="50dp"
    />

    <ImageView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/ic_launcher" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

在你的清单中,有一个拼写错误:

<action android:name="android.service.wallpaperWallpaperService" >

缺少&#34;。&#34;包名和类名之间:

<action android:name="android.service.wallpaper.WallpaperService" >