无法获取显示动态壁纸的设置按钮

发布于 2024-10-20 08:57:08 字数 3820 浏览 5 评论 0原文

我想向我创建的动态壁纸添加设置。我遗漏了有关 SharedPreferences 工作原理的一些非常基本的内容。代码和 XML 基于立方体动态壁纸示例,我无法弄清楚我做错了什么。我的问题是,当我从列表中选择动态壁纸时,没有显示“设置”按钮。仅显示“设置壁纸”按钮。我怀疑我搞砸了 XML 中的某些内容。

这是一个非常简单的动态壁纸,我只是为了摆弄设置而制作的。它所做的只是将背景设置为蓝色或绿色(并且该部分有效)。

我认为相关的代码和xml如下:

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(壁纸服务)

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);    
    }
    // ...
}

那么我错过了什么?

另外,我的 XML/代码的一部分被自动格式化所吞噬。有没有办法让它将文本块视为文字,这样就不会格式化它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还给你自由 2024-10-27 08:57:08

在您的 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 是您需要针对动态壁纸 XML 文件设置的参数。

祝你好运 :)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文