如何维护脱机应用程序android的会话

时间:2015-01-06 10:16:09

标签: java android

是否有一种标准的方法来处理Android应用程序的会话,其中登录只发生一次并且除了同步之外大部分时间都处于脱机状态? (对于这个用例,我们将会话定义为应用程序的启动和退出。会话不依赖于登录和注销)

1 个答案:

答案 0 :(得分:3)

您可以使用Android中的共享应用程序首选项来维护会话存储, 在这里,我将共享一个完整的PreferenceHandler类,我主要在android应用程序中使用,

public class PreferenceHelper {

    SharedPreferences preferences;
    Context context;
    Editor editor;
    public static final String PREFERENCES_NAME = "YourApplicationName";

    public PreferenceHelper(Context context) {
        preferences = context.getSharedPreferences(PREFERENCES_NAME,
                Context.MODE_PRIVATE);
    }

    public void addPreference(String key, String value) {
        editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void addPreference(String key, Set<String> values) {
        editor = preferences.edit();
        editor.putStringSet(key, values);
        editor.commit();
    }

    public void addPreference(String key, int value) {
        editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public void addPreference(String key, float value) {
        editor = preferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public void addPreference(String key, boolean value) {
        editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public void addPreference(String key, long value) {
        editor = preferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public void editPreference(String key, String value) {
        editor = preferences.edit();
        if (preferences.contains(key)) {
            editor.remove(key);
        }
        addPreference(key, value);
    }

    public void editPreference(String key, Set<String> value) {
        editor = preferences.edit();
        if (preferences.contains(key)) {
            editor.remove(key);
        }
        addPreference(key, value);
    }

    public void editPreference(String key, int value) {
        editor = preferences.edit();
        if (preferences.contains(key)) {
            editor.remove(key);
        }
        addPreference(key, value);
    }

    public void editPreference(String key, float value) {
        editor = preferences.edit();
        if (preferences.contains(key)) {
            editor.remove(key);
        }
        addPreference(key, value);
    }

    public void editPreference(String key, boolean value) {
        editor = preferences.edit();
        if (preferences.contains(key)) {
            editor.remove(key);
        }
        addPreference(key, value);
    }

    public void editPreference(String key, long value) {
        editor = preferences.edit();
        if (preferences.contains(key)) {
            editor.remove(key);
        }
        addPreference(key, value);
    }

    public String getStringValue(String key) {
        String value = preferences.getString(key, "");
        return value;
    }

    public Set<String> getValues(String key) {
        Set<String> values = preferences.getStringSet(key, null);
        return values;
    }

    public int getIntValue(String key) {
        int value = preferences.getInt(key, -1);
        return value;
    }

    public boolean getBooleanValue(String key) {
        boolean value = preferences.getBoolean(key, false);
        return value;
    }

    public float getFloatValue(String key) {
        float value = preferences.getFloat(key, -1);
        return value;
    }

    public Long getLongValue(String key) {
        Long value = preferences.getLong(key, -1);
        return value;
    }

    public void removeKey(String key) {
        preferences.edit().remove(key);
        preferences.edit().commit();
    }

    public void clearAllPreferences() {
        preferences.edit().clear();
        preferences.edit().commit();
    }
}

要在您的应用程序中调用并使用它,只需创建此类的对象,

PreferenceHelper preferences=new PreferenceHelper(getApplicationContext());

如果您正在使用Segments或任何其他子布局,请使用此方法

PreferenceHelper preferences = new PreferenceHelper(getActivity().getApplicationContext());

以下是我按照登录要求使用此内容的示例

if (preferences.getBooleanValue("is_logged")) {
            preferences
                    .addPreference("last_login", DateHelper.getCurrentDate());
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }

设置首选项中的值我在登录时使用此方法, 在Login方法中使用此,

preferences.addPreference("is_logged", true);
preferences.addPreference("email", email);
preferences.addPreference("user_name", username);
preferences.addPreference("password", password);
preferences.addPreference("last_login",
DateHelper.getCurrentDate());
int uid = JsonHelper.getReturnID(result);
boolean is_admin, is_manager;
is_admin = JsonHelper.getBoolean(result, "is_admin");
is_manager = JsonHelper.getBoolean(result, "is_manager");
preferences.addPreference("is_admin", is_admin);
preferences.addPreference("is_manager", is_manager);
preferences.addPreference("uid", uid);
String phone, name;
phone = JsonHelper.getStringFromResult(result, "phone");
name = JsonHelper.getStringFromResult(result, "name");
preferences.addPreference("phone", phone);
preferences.addPreference("name", name);
startActivity(new Intent(StartingActivity.this,
MainActivity.class));

希望这能解决你的问题。