Android Tutorial Android Live Wallpaper tutorial

The user can set live wallpapers by long pressing on the home screen and choosing Wallpapers - Live Wallpapers.
Creating a live wallpaper is not too difficult.
A service is responsible for handling the wallpaper events and drawing the wallpaper.

There can be several instances of the same wallpaper at the same time. For example the user can set your wallpaper as the home screen wallpaper and also see a demo of your wallpaper in the wallpaper preview dialog.

LiveWallpaper library contains two objects: LWManager and LWEngine.
LWManager is responsible for raising the events.
The first parameter of each event is of type LWEngine. LWEngine represents a specific wallpaper instance.
LWEngine includes a Canvas property which is used to draw the wallpaper.
When you finish drawing you must call LWEngine.Refresh or LWEngine.RefreshAll. Otherwise the drawing will not appear.

As there could be several different engines, it is convenient to work with the local engine.
LWEngine includes a Tag property which you can use to store data specific to that engine.

LWManager includes an internal timer that you can use if you need to do animations. The Tick event will only be raised for visible wallpapers. This is important to conserve battery.

For example the following code draws the time on the wallpaper:
B4X:
Sub Process_Globals
   Dim lwm As LWManager
End Sub

Sub Service_Create
   lwm.Initialize("lwm", True)
   lwm.StartTicking(1000) 'tick every second
End Sub

Sub LWM_Tick (Engine As LWEngine)
   Engine.Canvas.DrawColor(Colors.Black) 'Erase the background
   Engine.Canvas.DrawText(DateTime.Time(DateTime.Now), _
      300dip, 100dip, Typeface.DEFAULT_BOLD, 40, Colors.White, "LEFT")
   Engine.RefreshAll
End Sub
SS-2011-11-15_11.23.06.png


Offsets

On most devices the wallpaper virtual size is wider than a single screen. When the user moves to a different screen the offset changes.
You can use the OffsetChanged event to handle those changes.
LWEngine.FullWallpaperWidth / Height return the full size of the wallpaper.
LWEngine.CurrentOffsetX / Y return the current position.
Note that the wallpaper scrolls less than the foreground layer with the icons.

The LiveWallpaperImage demonstrates how to use those properties to display an image over the full wallpaper.

LWManager events

SizeChanged - Raised when the engine is first ready and when the screen size changes (for example when the orientation changes).
VisibilityChanged - Raised when a wallpaper becomes visible or invisible.
Touch - Raised when the user touches the wallpaper.
Tick - The internal timer tick event.
OffsetChanged - Raised when the wallpaper offsets change.
EngineDestroyed - Raised when an engine is destroyed.

Configuration

Manifest editor:
B4X:
AddApplicationText(
<service
        android:label="My Livewallpaper"
        android:name="anywheresoftware.b4a.objects.WallpaperInternalService"
        android:permission="android.permission.BIND_WALLPAPER"
        android:exported="true">
        <intent-filter >
            <action android:name="android.service.wallpaper.WallpaperService" />
        </intent-filter>
        <meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper" />
</service>
)
CreateResource(xml, wallpaper.xml,
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
  android:thumbnail="@drawable/icon"
/>
)

Updates

- v1.10 - fixes issues related to targetSdkVersion=34. Previous versions will not work.
- Example was updated. It is based on B4XPages. The XML resource is created in the manifest editor directly.

1728281678475.png


Copy the updated library to the internal libraries folder.
 

Attachments

  • WallpaperExample.zip
    18 KB · Views: 108
  • LiveWallpaper.zip
    8.9 KB · Views: 106
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
hi @Erel
i have try your sample, but dont work on my moto g, i dont make any change, i open the app , select one image on galery and later i see a toast image set, but dont work dont put my image at background *-*
LiveWallpaperImage.zip
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Sub ActivateAA(BG As Canvas, AAstate As Boolean ) As Boolean 
	Dim Obj1 As Reflector
	If AntiAliasing Then 
		'Try
			Obj1.Target = BG
			Obj1.Target = Obj1.GetField("paint")
			Obj1.RunMethod2("setAntiAlias", AAstate, "java.lang.boolean")
			Return AAstate
		'Catch
		'End Try
	End If
End Sub
[/code[
 

Troberg

Well-Known Member
Licensed User
Longtime User
I've already tried that, and, while it runs without errors, it doesn't give me any antialiasing either.

It doesn't matter which lwm engine event I run it in, no result whatsoever.
 

koaunglay

Member
Licensed User
Longtime User
Hi! All friends! I'm trying with three module(Main, userP, WallpaperService).
This is my code from wallpaper.xml
-----------------
B4X:
<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
  android:thumbnail="@drawable/icon" 
  android:settingsActivity="com.aunglw2.userP"
/>
---------------------------
This is my code from manifest editor
-------------------------------------
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.
AddApplicationText(
<service
android:label="HI!"
android:name="anywheresoftware.b4a.objects.WallpaperInternalService"
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/wallpaper"/>
</service>
)
SetActivityAttribute(userP,android:exported, true)
But when I click settings.. button in preview, my app is not work. It show " Unfortunately, Live Wallpaper Picker has stopped."
How should I do? Help some body! Thanks all....
 

koaunglay

Member
Licensed User
Longtime User
This is from my log. I think no error. I don't know...
=============
** Service (wallpaperservice) Create **


** Service (wallpaperservice) Start **


** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Activity (main) Pause, UserClosed = false **


** Activity (userp) Create, isFirst = true **


Downsampling image due to lack of memory.


Downsampling image due to lack of memory.


** Activity (userp) Resume **


Downsampling image due to lack of memory.


** Activity (userp) Pause, UserClosed = true **

===============Help me please................
 
Top