下面的代码是我为我的Windows手机应用程序编写的设置类。它非常易于使用:
using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
namespace myAppName
{
public class AppSettings
{
// Our isolated storage settings
IsolatedStorageSettings settings;
// The isolated storage key names of our settings
const string InstalledonSettingKeyName = "InstalledonSetting";
const string SeenTutorialSettingKeyName = "SeenTutorialSetting";
const string FontSizeSettingKeyName = "FontSizeSetting";
const string FontTypeSettingKeyName = "FontTypeSetting";
// The default value of our settings
const long InstalledonSettingDefault = 0;
const bool SeenTutorialSettingDefault = false;
const int FontSizeSettingDefault = 30;
const string FontTypeSettingDefault = "";
/// <summary>
/// Constructor that gets the application settings.
/// </summary>
public AppSettings()
{
try
{
// Get the settings for this application.
settings = IsolatedStorageSettings.ApplicationSettings;
}
catch (Exception e)
{
Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
}
}
/// <summary>
/// Update a setting value for our application. If the setting does not
/// exist, then add the setting.
/// </summary>
/// <param name="Key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (settings.Contains(Key))
{
// If the value has changed
if (settings[Key] != value)
{
// Store the new value
settings[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
settings.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
/// <summary>
/// Get the current value of the setting, or if it is not found, set the
/// setting to the default setting.
/// </summary>
/// <typeparam name="valueType"></typeparam>
/// <param name="Key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
{
valueType value;
// If the key exists, retrieve the value.
if (settings.Contains(Key))
{
value = (valueType)settings[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}
return value;
}
/// <summary>
/// Save the settings.
/// </summary>
public void Save()
{
settings.Save();
}
public long InstalledonSetting
{
get
{
return GetValueOrDefault<long>(InstalledonSettingKeyName, InstalledonSettingDefault);
}
set
{
if (AddOrUpdateValue(InstalledonSettingKeyName, value))
{
Save();
}
}
}
public int FontSizeSetting
{
get
{
return GetValueOrDefault<int>(FontSizeSettingKeyName, FontSizeSettingDefault);
}
set
{
if (AddOrUpdateValue(FontSizeSettingKeyName, value))
{
Save();
}
}
}
public string FontTypeSetting
{
get
{
return GetValueOrDefault<string>(FontTypeSettingKeyName, FontTypeSettingDefault);
}
set
{
if (AddOrUpdateValue(FontTypeSettingKeyName, value))
{
Save();
}
}
}
public bool SeenTutorialSetting
{
get
{
return GetValueOrDefault<bool>(SeenTutorialSettingKeyName, SeenTutorialSettingDefault);
}
set
{
if (AddOrUpdateValue(SeenTutorialSettingKeyName, value))
{
Save();
}
}
}
}
}
现在使用这个类在另一个类中非常容易
像这样namespace myAppName
{
public partial class MyActivity : PhoneApplicationPage
{
private AppSettings settings = new AppSettings();
public MyActivity()
{
InitializeComponent();
if (!settings.SeenTutorialSetting) ShowTutorial();
}
}
现在我担心的是我可以对Android java类做同样的事情:
package com.example.myappname;
public class AppSettings{
String InstalledonSettingKeyName = "InstalledonSetting";
String SeenTutorialSettingKeyName = "SeenTutorialSetting";
String FontSizeSettingKeyName = "FontSizeSetting";
String FontTypeSettingKeyName = "FontTypeSetting";
// The default value of our settings
ong InstalledonSettingDefault = 0;
bool SeenTutorialSettingDefault = false;
int FontSizeSettingDefault = 30;
String FontTypeSettingDefault = "";
public AppSettings(){}
我不喜欢这样做:
if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("myapp_finished_loading", false))
{
}
甚至是这样:
SharedPreferences vSettings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
long used_time_l = System.currentTimeMillis() - vSettings.getLong("myapp_first_data", 0);
对于android java类中的任何关于继承的帮助都将非常感激。如果你必须downvote让我知道原因。
答案 0 :(得分:1)
您可以在创建对象上初始化一次首选项并稍后使用
class Settings {
public static String PARAM_PARAM1 = "param1";
public static String PARAM_PARAM2 = "param2";
private static String DEFAULT_PARAM1 = "hello";
private static boolean DEFAULT_PARAM2 = false;
private SharedPreferences prefs;
public Settings(Context ctx){
prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
}
public String getParam1(){
return prefs.getString(PARAM_PARAM1, DEFAULT_PARAM1);
}
public void setParam1(String param1){
prefs.edit()
.putString(PARAM_PARAM1, param1)
.apply();
}
public boolean getParam2(){
return prefs.getBoolean(PARAM_PARAM1, DEFAULT_PARAM2);
}
public void setParam2(boolean param2){
prefs.edit()
.putBoolean(PARAM_PARAM2, param2)
.apply();
}
}