从动态壁纸中的设置页面检索数据

时间:2015-02-28 10:11:14

标签: android settings live-wallpaper

请将以下代码视为Android中动态壁纸的设置页面:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/livewallpaper_settings"
    android:key="livewallpaper_settings">

    <ListPreference
        android:key="livewallpaper_testpattern"
        android:title="@string/livewallpaper_settings_title"
        android:summary="@string/livewallpaper_settings_summary"
        android:entries="@array/livewallpaper_testpattern_names"
        android:entryValues="@array/livewallpaper_testpattern_prefix"/>
    <CheckBoxPreference android:key="livewallpaper_movement"
        android:summary="@string/livewallpaper_movement_summary"
        android:title="@string/livewallpaper_movement_title"
        android:summaryOn="Moving test pattern"
        android:summaryOff="Still test pattern"/>
</PreferenceScreen>

显示设置页面,显示设置的所有内容都可以。如何在创建动态壁纸时保存此设置并使用它们?

另外,是否真的读取onCreate方法中的设置?

1 个答案:

答案 0 :(得分:0)

我找到了答案:

package ca.jvsh.livewallpaper;

import ca.jvsh.livewallpaper.R;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class LiveWallpaperSettings extends PreferenceActivity
    implements SharedPreferences.OnSharedPreferenceChangeListener
{
    @Override
    protected void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        getPreferenceManager().setSharedPreferencesName(LiveWallpaper.SHARED_PREFS_NAME);
        addPreferencesFromResource(R.xml.livewallpaper_settings);
        getPreferenceManager().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onDestroy()
    {
        getPreferenceManager().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
        super.onDestroy();
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key)
    {
    }
}

这是设置页面的java类。 这是为了阅读设置:

    TestPatternEngine()
    {
        ...

        mPreferences = LiveWallpaper.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
        mPreferences.registerOnSharedPreferenceChangeListener(this);
        onSharedPreferenceChanged(mPreferences, null);
    }

    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key)
    {
        mShape = prefs.getString("livewallpaper_testpattern", "smpte");
        mMotion = prefs.getBoolean("livewallpaper_movement", true);
        readColors();
    }