Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在social auth中存储facebook访问令牌_Android_Facebook_Integration_Socialauth - Fatal编程技术网

Android 如何在social auth中存储facebook访问令牌

Android 如何在social auth中存储facebook访问令牌,android,facebook,integration,socialauth,Android,Facebook,Integration,Socialauth,我正在使用social auth集成facebook、google等社交应用程序。我成功地进行了授权,并且在logcat中打印了访问令牌,但我想存储它们以便发送到api class SignUp extends Activity { SocialAuthAdapter adapter; public void onCreate(Bundle SavedBundleInstanceState ) { adapter = new SocialAuthAdapter(new Res

我正在使用social auth集成facebook、google等社交应用程序。我成功地进行了授权,并且在logcat中打印了访问令牌,但我想存储它们以便发送到api

class SignUp extends Activity
{
  SocialAuthAdapter adapter;
  public void onCreate(Bundle SavedBundleInstanceState )
  { 
   adapter = new SocialAuthAdapter(new ResponseListener());
   adapter.authroize(SignUp.this,Provider.Facebook);

  }

通过这段代码,我在logcat中获得了访问令牌,但不知道如何存储它。

访问令牌存储在会话类中

Session session = Session.getActiveSession();
String accessToken = session.getAccessToken();

您可以使用最简单的方法
共享首选项
。。或者使用
会话
。。。您可以使用
键值保存和检索
字符串

SessionManager.java

 public class SessionManager {
// Shared Preferences
SharedPreferences pref;

// Editor for Shared preferences
Editor editor;

// Context
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

// Sharedpref file name
private static final String PREF_NAME = "wlm";

// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
private static final String STATUS = "status";
private static final String STATUS_COLOR = "status_color";



// User name (make variable public to access from outside)
public static final String KEY_NAME = "name";

// Email address (make variable public to access from outside)
public static final String KEY_EMAIL = "email";

// Constructor
public SessionManager(Context context){
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}
/**
 * Create login session
 * */
public void createLoginSession(String name, String email){
    // Storing login value as TRUE
    editor.putBoolean(IS_LOGIN, true);

    // Storing name in pref
    editor.putString(KEY_NAME, name);

    // Storing email in pref
    editor.putString(KEY_EMAIL, email);

    // commit changes
    editor.commit();
}   

/**
 * Check login method wil check user login status
 * If false it will redirect user to login page
 * Else won't do anything
 * */
public void checkLogin(){
    // Check login status
    if(!this.isLoggedIn()){
        // user is not logged in redirect him to Login Activity
        //           Intent i = new Intent(_context, LoginScreen.class);
        //           // Closing all the Activities
        //           i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //            
        //           // Add new Flag to start new Activity
        //           i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //            
        //           // Staring Login Activity
        //           _context.startActivity(i);
    }

}

/**
 * Get stored session data
 * */
public HashMap<String, String> getUserDetails(){
    HashMap<String, String> user = new HashMap<String, String>();
    // user name
    user.put(KEY_NAME, pref.getString(KEY_NAME, null));

    // user email id
    user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));

    // return user
    return user;
}

/**
 * Clear session details
 * */
public void logoutUser(){
    // Clearing all data from Shared Preferences
    editor.clear();
    editor.commit();

    // After logout redirect user to Loing Activity
    //       Intent i = new Intent(_context, LoginScreen.class);
    //       // Closing all the Activities
    //       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    //        
    //       // Add new Flag to start new Activity
    //       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //        
    //       // Staring Login Activity
    //       _context.startActivity(i);
}

/**
 * Quick check for login
 * **/
// Get Login State
public boolean isLoggedIn(){
    return pref.getBoolean(IS_LOGIN, false);
}
并用

String str = session.pref.getString("your key", "");
还要为SessionManager中的每个条目定义键,如

 private static final String ANY_NAME= "your key";

你可以使用android上的许多存储选项,如共享首选项、SQLite数据库等@Mourice u r不了解我的问题。实际上,通过上述代码,我可以在logcat上打印访问令牌,但不知道如何将它们放入我的活动中,以便我可以使用它们。我使用social auth进行集成
 private static final String ANY_NAME= "your key";