Can't get settings button to display for live wallpaper

怎甘沉沦 提交于 2020-01-01 14:57:40

问题


I want to add settings to a live wallpaper I created. I am missing something very fundamental about how SharedPreferences work. The code and XML are based on the cube live wallpaper example and I can’t figure out what I did wrong. My problem is that no "Settings" button shows up when I choose my live wallpaper from the list. Only the "Set Wallpaper" button is displayed. I suspect that I screwed up something in the XML.

This is a very simple live wallpaper I made just to fool around with settings. All it does is set the background to either blue or green (and that part works).

What I believe to be the pertinent code and xml follows:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.BGWallpaper"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-feature android:name="android.software.live_wallpaper" />

    <application
        android:icon="@drawable/icon" android:label="@string/AppName">

        <service
            android:icon="@drawable/icon"
            android:label="@string/AppName"
            android:name="com.android.BGWallpaper.BlueGreen"
            android:permission="android.permission.BIND_WALLPAPER" android:debuggable="false">
            <intent-filter android:priority="1">
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data android:name="android.service.wallpaper" android:resource="@xml/bg" />
        </service>
        <activity
            android:label="BGSettings"
            android:name="com.android.BGWallpaper.BGPrefs"
            android:theme="@android:style/Theme.Light.WallpaperSettings"
            android:exported="true">
        </activity>
    </application>
</manifest>

Preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="Title Preference"
        android:key="BGSettings">
    <ListPreference
            android:key="background"
            android:title="Background Title"
            android:summary="Background Summary"
            android:entries="@array/BackgroundChoices"
            android:entryValues="@array/BackgroundChoices" />
</PreferenceScreen>

bg.xml

<?xml version="1.0" encoding="utf-8"?>
<wallpaper 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:thumbnail="@drawable/icon"/>

BlueGreen.java (the wallpaper service)

public class BlueGreen extends WallpaperService 
{
    public static final String strSharedPrefs="BGSettings";

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

    class BGEngine extends Engine  implements SharedPreferences.OnSharedPreferenceChangeListener
    {
        private SharedPreferences msPrefs;

        private final Runnable mDraw = new Runnable() 
        {
            public void run() 
            {
                draw();
           }
        };

        BGEngine()
        {
            msPrefs = BlueGreen.this.getSharedPreferences(strSharedPrefs, MODE_PRIVATE);
            msPrefs.registerOnSharedPreferenceChangeListener(this);
            onSharedPreferenceChanged(msPrefs, null);
        }
    }
    // ...
}

BGPrefs.java:

public class BGPrefs extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener 
{    
    @Override
    protected void onCreate(Bundle icicle) 
    {
        super.onCreate(icicle);
        getPreferenceManager().setSharedPreferencesName(BlueGreen.strSharedPrefs);
        addPreferencesFromResource(R.xml.preferences);
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);    
    }
    // ...
}

So what am I missing?

Also, part of my XML/code is being eaten by the automatic formatting. Is there a way to have it treat a block of text as literal so it doesn't format it?


回答1:


In your bg.xml...

<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="something"
    android:thumbnail="@drawable/icon"
    android:description="something"
    android:settingsActivity="com.android.BGWallpaper.BGPrefs">
</wallpaper>

android:settingsActivity is the param that you need to set against your live wallpaper XML file.

Good luck :)



来源:https://stackoverflow.com/questions/5188742/cant-get-settings-button-to-display-for-live-wallpaper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!