活动不叫服务

时间:2015-02-01 19:27:14

标签: android android-activity service

我是android编程的新手。我正在尝试创建一个动态壁纸应用程序。继Lars Vogella的教程之后。我创建了一个活动,一个按钮和一个服务。当我点击按钮时,它会进入onClick功能。但它没有启动服务。我真的很困惑。

    public class SetWallpaperActivity extends Activity {
    private final String TAG = "SetWallpaperActivity";
    public void onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        setContentView(R.layout.main);
    }

    public void onClick(View view){
        Toast.makeText(this, "Entered onClick",Toast.LENGTH_LONG).show();
        Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
        intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this,MyWallpaperService.class));
        startActivity(intent);
    }
}

我的服务类看起来像这样

    public class MyWallpaperService extends WallpaperService {
    private final String TAG = "MyWallpaperService";
    public Engine onCreateEngine(){
        Log.v(TAG, "Creating Engine");
        return new RenderEngine();
    }

    public class RenderEngine extends Engine{
        private final Handler handler = new Handler();
        private final Runnable drawRunner = new Runnable(){
            public void run(){
                draw();
            }
        };
        private Paint paint = new Paint();
        private boolean visible = true;
        private int width;
        int height;


        public RenderEngine(){
            paint.setAntiAlias(true);
            paint.setColor(Color.WHITE);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(10.0f);
            handler.post(drawRunner);
        }

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

        @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);
        }

        public void draw(){
            SurfaceHolder holder = getSurfaceHolder();
            Canvas c = null;
            Log.v(TAG,"Entered Draw");
            try{
                c = holder.lockCanvas();
                if(c != null){
                    Log.v(TAG,"Entered Try");
                }
            }finally{
                if(c != null){
                    holder.unlockCanvasAndPost(c);
                }
            }
            handler.removeCallbacks(drawRunner);
            if(visible){
                handler.postDelayed(drawRunner,5000);
            }
        }
    }
}

任何人都可以帮我搞清楚吗?我被困在这里已经3天了。

编辑:

<service
    android:name=".MyWallpaperService"
    android:enabled="true"
    android:label="Wallpaper Example"
    android:permission="android.permission.BIND_WALLPAPER">

    <intent-filter>
        <action android:name="android.intent.action.WallpaperService"/>
    </intent-filter>

    <meta-data
        android:name="android.service.wallpaper"
        android:resource="@xml/mywallpaper">

    </meta-data>

</service>

2 个答案:

答案 0 :(得分:1)

确保您的服务在清单中声明并且具有相应的权限:

  

该服务需要android.permission.BIND_WALLPAPER权限   并且必须通过意图过滤器进行注册   android.service.wallpaper.WallpaperService动作。

另外,不要忘记在清单中声明它

<service android:enabled="true" android:name=".MyWallPaperservice" />

答案 1 :(得分:0)

好的,这很傻。我将该部分放在清单文件中的区域之外。现在感觉很蠢。