Livewallpaper
From Android 2.1, Livewallpaper is supported. Livewallpaper is home screen, and lock screen wallpaper app?, umm… it’s difficult to say app, maybe service.
Let’s start No Activity template
When we create Livewallpaper, we don’t need to create Activity. So, we can select No Activity from Android Studio template.
Sample
Let’s start development.
In this time, I just explain basic sample. (Support 1 image in local to show)
This is step
- Import image
- Develop Service
- Prepare wallpaper configuration xml
- Change AndroidManifest.xml to support wallpaper service
Sample
LiveWallSample.kt
class LiveWallSample : WallpaperService() { override fun onCreateEngine(): Engine { return WallPaperEngine() } private inner class WallPaperEngine : Engine() { private var visible : Boolean = true private val handler : Handler = Handler() private val runner : Runnable = Runnable { drawFrame() } private var width : Int = 0 private var height : Int = 0 override fun onCreate(surfaceHolder: SurfaceHolder?) { super.onCreate(surfaceHolder) } override fun onSurfaceChanged( holder: SurfaceHolder?, format: Int, width: Int, height: Int ) { super.onSurfaceChanged(holder, format, width, height) this.width = width this.height = height } override fun onSurfaceDestroyed(holder: SurfaceHolder?) { super.onSurfaceDestroyed(holder) this.visible = false handler.removeCallbacks(runner) } override fun onVisibilityChanged(visible: Boolean) { this.visible = visible if (visible) { handler.post(runner) } else { handler.removeCallbacks(runner) } } override fun onTouchEvent(event: MotionEvent?) { super.onTouchEvent(event) } /* Helper Methods */ private fun drawFrame() { var canvas : Canvas? = null try { canvas = surfaceHolder.lockCanvas() canvas?.let { // Draw something drawImage(it) } } finally { canvas?.let { surfaceHolder.unlockCanvasAndPost(it) } } } private fun drawImage(canvas: Canvas) { val bitmap : Bitmap = BitmapFactory.decodeResource(resources, R.drawable.lion) canvas.drawBitmap(bitmap, null, Rect(0, 0, width, height), null) } // onSurfaceChanged // onVisibilityChanged // onSurfaceDestroyed // onCreate } }
This is main service code. Extends WallpaperService class and implement
onCreateEngine, and Prepare Engine to draw something in canvas.
xml/live_wallpaper.xml
Livewall paper definition XML.
<?xml version="1.0" encoding="utf-8"?> <wallpaper xmlns:android="http://schemas.android.com/apk/res/android" android:thumbnail="@drawable/logo"> </wallpaper>
AndroidManifest.xml
Set service as application layer (No Activity)
Also support above XML wallpaper definition.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.daiji110.wallpapersample"> <uses-feature android:name="android.software.live_wallpaper" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name=".LiveWallSample" android:label="@string/app_name" android:permission="android.permission.BIND_WALLPAPER"> <intent-filter> <action android:name="android.service.wallpaper.WallpaperService" /> </intent-filter> <meta-data android:name="android.service.wallpaper" android:resource="@xml/live_wallpaper" /> </service> </application> </manifest>
How to Test?
This sample is not application, and no Activity. How to install this?
O.K. Actually, you need to install (run) this program into simulator.

To install this service into simulator, you can press Green ▶︎ button next to simulator select.
Press this and this is installed in simualtor
Check from Wallpaper list
Go to home screen. and you can do long press and can select “Wallpapers”

And we can look for icon I set from wallpaper list

Yeah, WallpaperSample is mine. Please select it.
O.K. That’s it.
コメント