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: 75
  • LiveWallpaper.zip
    8.9 KB · Views: 76
Last edited:

shashkiranr

Active Member
Licensed User
Longtime User
Erel i downloaded the Library 2 days ago. Could you kindly provide the URL for the newer version.
 

hasexxl1988

Active Member
Licensed User
Longtime User
Problems mit Multipictures

Hello,
is there a possibility that will play automatically DirAssent 10 images from the directory in a loop as LWP?

Thanks

EDIT: Okay, problem solved itself. I simply modified the example ball: D
 
Last edited:

tuhatinhvn

Active Member
Licensed User
Longtime User
With exam livewallpaperball Live Wallpaper APi>16

I want to use this function to change LW

But put extra i dont know value of android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT??

I tested below but nothing run :((

B4X:
Dim i As Intent
i.Initialize("android.service.wallpaper.CHANGE_LIVE_WALLPAPER", "")
i.PutExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", "anywheresoftware.b4a.samples.livewallpaperball/.WallpaperService")
StartActivity(i)


Anyone help me how to config value of android.service.wallpaper.extra
 

tuhatinhvn

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    ToastMessageShow("Please go to the home screen and add the Live Wallpaper.", True)
Dim Intent1 As Intent
Intent1.Initialize("android.service.wallpaper.CHANGE_LIVE_WALLPAPER","")
Intent1.PutExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT","anywheresoftware.b4a.samples.livewallpaperball/anywheresoftware.b4a.objects.WallpaperInternalService")

StartActivity(Intent1)

Activity.Finish
End Sub

But dont work :(((

my device is 4.1.2 JB
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Dim Intent1 As Intent
   Intent1.Initialize("android.service.wallpaper.CHANGE_LIVE_WALLPAPER","")
   Dim cn As JavaObject
   cn.InitializeNewInstance("android.content.ComponentName", _
     Array As Object("anywheresoftware.b4a.samples.livewallpaperball", "anywheresoftware.b4a.objects.WallpaperInternalService"))
   Intent1.PutExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT",cn)
   StartActivity(Intent1)
   Activity.Finish

http://www.b4x.com/search?query=JavaObject
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi Erel,

I wanted a settings button in the live wallpaper selection. I went through the posts in the Page 1 and got it working. After i updated my phone Note 2 to 4.3 if i click on the settings button it gives a toast message " Application is not installed on phone" . I dint change anything.

I have the Live Wallpaper installed and it is working fine in my phone.

Regards,
SK
 

shashkiranr

Active Member
Licensed User
Longtime User
Yes Erel i tried Multiple times but it is not working. The Livewallpaper works fine but settings is not opening. I think it is a bug in Samsung 4.3 update since many issues are coming up for this but i am not sure.
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi Erel,

I found this difference since i am using LibGDX Wallpaper template, the manifest Editor XML is different as mentioned in LibGDX tutorial . The android name is different.

B4X:
AddApplicationText(
<!-- ******** Add the internal service declaration - you can change android:label  ******* -->
<service
        android:label="Time"
        android:name="anywheresoftware.b4a.libgdx.lgLiveWallpaper"
        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>
<!-- End of internal service declaration -->
)

Am i doing something wrong or is the problem exists in the declaration itself??

Regards,
SK
 

holdemadvantage

Active Member
Licensed User
Longtime User
Hi all, my problem now is to add a settings activity to my app.

I've read the solution and add this line into wallpaper.xml file

B4X:
<?xml version="1.0" encoding="utf-8"?><wallpaper xmlns:android="http://schemas.android.com/apk/res/android"  android:thumbnail="@drawable/icon"  android:settingsActivity="anywheresoftware.b4a.samples.livewallpaperimage.main2"/>

mian 2 is an activity that load a layout
When i open LWP in my galaxy note 3 and click on livewallpaper image icon i see a settings button, clicking on it i receive a warning message, something like "the software select live wallpaer has been stopped"
I upload the zip file http://www.omahabrain.com/b4a/testlwi.zip

Thanks in advance for your help
 
Top