Tuesday, May 24, 2011

Live Wallpaper

URL:http://android.arnodenhond.com/tutorials/live-wallpaper 

http://www.codeproject.com/KB/android/AndroidLiveWallpaper.aspx

Live Wallpaper

A feature of Android 2.1 (nexus) is Live Wallpapers.
This tutorial shows how to make them.
A Live Wallpaper is a subclass of WallpaperService and an Engine inner class.
Optionally an Activity (for settings) can be added to the package.

In the manifest.xml file, add these elements inside the application tag
<service
    android:label="@string/mylwpservice"
    android:name=".MyLWPService"
    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/mylwp" />
</service>
<activity
    android:label="@string/mylwpactivity"
    android:name=".MyLWPActivity"
    android:theme="@android:style/Theme.Light.WallpaperSettings"
    android:exported="true">
</activity>
And don't forget to set the ApiLevel to 7.

The xml resource specified in the meta-data of the service must point to the Activitiy.
<wallpaper
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:settingsActivity="mylwpexample.MyLWPActivity"/>
The user can start the activity by selecting the settings button in the preview screen or directly in the wallpaper settings on the home screen.
To have the wallpaper change without restarting after changing the settings, the engine can implement the SharedPreferences.OnSharedPreferenceChangeListener interface to receive such changes.
Next, create the WallpaperService subclass called MyLWPService.
It must have an inner class which subclasses Engine in WallpaperService and an onCreateEngine method to return it.
public class MyLWPService extends WallpaperService {

    @Override
    public Engine onCreateEngine() {
           return new MyLWPEngine();
    }

    class MyLWPEngine extends Engine {
        ...
    }

}

No comments:

Post a Comment