Aayush lamsal
Aayush lamsal

Reputation: 21

livewallpaper Double Tap to open an activity

I am new in android development. I want to implement a double tap event in my wallpaper so that when I double tap the wallpaper it opens the application.

here is the code where I have drawn the wallpaper image:

public class AbcService extends WallpaperService
{
    public void onCreate()
    {
        super.onCreate();
    }

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

    public Engine onCreateEngine()
    {
        return new wallpaperEngine();
    }

    class wallpaperEngine extends Engine
    {
        public Bitmap image1;

        wallpaperEngine()
        {
            image1 = BitmapFactory.decodeResource(getResources(), R.drawable.img_3);
        }

        public void onCreate(SurfaceHolder surfaceHolder)
        {
            super.onCreate(surfaceHolder);
        }

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

        void drawFrame()
        {

        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try
        {
            c = holder.lockCanvas();
            if (c != null)
            {
                c.drawBitmap(image1, 0, 0, null);

            }
        } finally
        {
            if (c != null) holder.unlockCanvasAndPost(c);
        }
    }
}

what to do now?? am i on right track?

Thank You in Advance..

Upvotes: 2

Views: 201

Answers (1)

A Honey Bustard
A Honey Bustard

Reputation: 3493

For a double Tap Action in a Live Wallpaper you can subclass a GestureDetector.SimpleOnGestureListener like this :

class wallpaperEngine extends Engine {
    ...
    ...
    private GestureDetector gestureListener;

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDoubleTap(MotionEvent e) {

            //start intent/application here

            return super.onDoubleTap(e);
        }
    }

Then create an Instance of that class, f.e. in your onCreate(SurfaceHolder surfaceHolder) Method and enable touch events in your Live Wallpaper :

   public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);

        gestureListener = new GestureDetector(getApplicationContext(), new GestureListener());
        setTouchEventsEnabled(true);

    }

Then @Override onTouchEvent :

    @Override
    public void onTouchEvent(MotionEvent event) {

       gestureListener.onTouchEvent(event);

    }

Thats the easiest way I can think of.

Upvotes: 1

Related Questions