如何从服务中检索偏好数据

How to retrieve preference data from the service

我正在开发一个 android 应用程序。

主要activity是一个偏好屏幕

public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

我的应用程序还包含一个 service,它会在 android 启动时自动启动。

在我的服务 class 的 onStart() 中,我正在尝试使用以下代码检索用户在首选项屏幕中保存的数据

public void onStart(Intent intent, int startId) {     
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    get_prefrence_settings((Preference) preferences);
    jniTask = new JniBackGround();
    jniTask.execute(new String[] { "http://www.easycwmp.org" });
}

private void get_prefrence_settings(Preference p) {
    if (p instanceof PreferenceGroup) {
        PreferenceGroup pGrp = (PreferenceGroup) p;
        for (int i = 0; i < pGrp.getPreferenceCount(); i++) {
            get_prefrence_settings(pGrp.getPreference(i));
        }
    } else {
        if (p instanceof ListPreference) {
            ListPreference listPref = (ListPreference) p;
            p.setSummary(listPref.getEntry());
            client_interface = (String) listPref.getEntry();
        }
        if (p instanceof CheckBoxPreference) {
            if (((TwoStatePreference) p).isChecked())
                enable = "1";
        }
        if (p instanceof EditTextPreference) {
            if (pref_key.equals("Time")) {
                time = editTextPref.getText();
            }
       }
    }
}

但是当我的服务启动时我遇到了崩溃,表明:

android.app.SharedPreferencesImpl can not be cast to android.preference.Preference

如何在不加载 MainActivity 界面的情况下从我的 onStart() 服务检索首选项设置?

How to retrieve the preference settings from my onStart() service without loading the MainActivity interface?

the SharedPreferences object 上调用 getString()getInt() 或相关的 getter 方法。