1

我正在尝试创建一个简单的 android 应用程序来创建动态壁纸。我已经创建了一个 Activity,我正在从 Activity 的 onCreate 方法启动壁纸服务。目前,我只是使用 Logcat 来监控服务是否已启动。我面临一个问题,即 WallpaperService 的 onCreateEngine 没有被调用,因为没有创建 Engine 的实例。我检查了现有的帖子,但似乎没有人遇到同样的问题。

主要活动

package com.example.rajeev.mylivewallpaperapplication;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("TAG","Starting Service");
        Intent i = new Intent(this, MyWallpaperService.class);
        if (Build.VERSION.SDK_INT > 15)
        {
            i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
            String pkg = WallpaperService.class.getPackage().getName();
            String cls = WallpaperService.class.getCanonicalName();
            i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(pkg, cls));
        }
        else
        {
            i.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
        }
        //startActivity(i);
        startService(i);
        //startActivityForResult(i, 0);

        //startService();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        stopService(new Intent(this, MyWallpaperService.class));
    }
}

MyWallpaperService 类

package com.example.rajeev.mylivewallpaperapplication;

import android.graphics.Canvas;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;

/**
 * Created by rajeev on 30/5/15.
 */
public class MyWallpaperService extends WallpaperService{
    private  String TAG = "My Wallpaper Application";
    @Override
    public void onCreate(){
        super.onCreate();
        Log.d(TAG,"Inside onCreate");
    }

    @Override
    public Engine onCreateEngine() {
        Log.d(TAG,"Creating Engine");
        return new MyEngine();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"Inside onDestroy");
    }

     class MyEngine extends Engine{
        private Handler handler;
        private SurfaceHolder holder;

        public MyEngine(){
            Log.d(TAG,"Inside Constructor of Engine");
            handler = new Handler();
            handler.post(drawFigure);
        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            Log.d(TAG,"Created Engine");
            super.onCreate(surfaceHolder);
            this.holder = surfaceHolder;
        }
        private Runnable drawFigure = new Runnable() {

            public void run() {
                draw();
            }
        };

        private void draw() {
            Canvas canvas = holder.lockCanvas();
            canvas.save();
            // Adjust size and position so that
            // the image looks good on your screen
            canvas.scale(3f, 3f);
            canvas.drawRGB(0,0,0);
            canvas.restore();
            holder.unlockCanvasAndPost(canvas);
            handler.removeCallbacks(drawFigure);
            handler.postDelayed(drawFigure, 100);

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

        @Override
        public void onVisibilityChanged(boolean visible) {
            handler.removeCallbacks(drawFigure);
            handler.postDelayed(drawFigure, 100);
        }

    }
}

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rajeev.mylivewallpaperapplication" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-feature
            android:name="android.software.live_wallpaper"
            android:required="true" >
        </uses-feature>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".MyWallpaperService"
            android:enabled="true"
            android:permission="android.permission.BIND_WALLPAPER" >
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
        </service>
    </application>

</manifest>
4

0 回答 0