Misca
Misca

Reputation: 469

Live Wallpaper in preview mode

I need my wallpaper to act differently when in preview mode (the screen with "Settings" and "Set .. "). How do i know when it's drawn there?

Upvotes: 10

Views: 4792

Answers (3)

Max Lebedev
Max Lebedev

Reputation: 47

I'll write in addition to represented answers. As the preview and non-preview engine instances could exist simultaneously, you can add two static instances and one local variable of your engine inside your WallpaperService class (sample in Kotlin):

    private var engine: OpenGLEngine? = null
    private set
    //...

    companion object {
       private var engineInstance: OpenGLEngine? = null
       private var previewEngineInstance: OpenGLEngine? = null
       //...
    }

and use them in overriding functions

     override fun onCreate(surfaceHolder: SurfaceHolder?) {
        super.onCreate(surfaceHolder)
        if (isPreview) {
            previewEngineInstance = this@OpenGLEngine
            engine = previewEngineInstance
        } else {
            engineInstance = this@OpenGLEngine
            engine = engineInstance
        }
        //...
    }

    override fun onDestroy() {
        if (isPreview) {
            engine = engineInstance
            previewEngineInstance = null
        } else {
            engine = previewEngineInstance
            engineInstance = null
        }
        //...
        super.onDestroy()
    }

This way you can always get the current engine instance in your WallpaperService and call its isPreview.

Upvotes: 1

FranMowinckel
FranMowinckel

Reputation: 4343

The isPreview() method can be called in the onCreate(SurfaceHolder holder) method of the implemented Engine. Not in the onCreateEngine method as the prior answer because the method is not ready.

Upvotes: 5

George Freeman
George Freeman

Reputation: 2260

Within onCreateEngine() you can use the isPreview() method.

Note that onCreateEngine() is "normally" called twice: once to create an instance for preview, and then again when you actually set the wallpaper.

Details here: http://developer.android.com/reference/android/service/wallpaper/WallpaperService.Engine.html

Upvotes: 15

Related Questions