package com.letv.watchball.share; import com.sina.weibo.sdk.auth.Oauth2AccessToken; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; /** * 该类用于保存Oauth2AccessToken到sharepreference,并提供读取功能 * @author xiaowei6@staff.sina.com.cn * */ public class AccessTokenKeeper { private static final String PREFERENCES_NAME = "com_weibo_sdk_android"; private static final String KEY_UID = "uid"; private static final String KEY_ACCESS_TOKEN = "access_token"; private static final String KEY_EXPIRES_IN = "expires_in"; /** * 保存accesstoken到SharedPreferences * @param context Activity 上下文环境 * @param token Oauth2AccessToken */ public static void keepAccessToken(Context context, Oauth2AccessToken token) { SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); Editor editor = pref.edit(); editor.putString(KEY_UID, token.getUid()); editor.putString(KEY_ACCESS_TOKEN, token.getToken()); editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime()); editor.commit(); } /** * 清空sharepreference * @param context */ public static void clear(Context context){ SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); Editor editor = pref.edit(); editor.clear(); editor.commit(); } /** * 从SharedPreferences读取accessstoken * @param context * @return Oauth2AccessToken */ public static Oauth2AccessToken readAccessToken(Context context){ Oauth2AccessToken token = new Oauth2AccessToken(); SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); token.setUid(pref.getString(KEY_UID, "")); token.setToken(pref.getString(KEY_ACCESS_TOKEN, "")); token.setExpiresTime(pref.getLong(KEY_EXPIRES_IN, 0)); return token; } }