2016-04-02 80 views
1

如何使此代碼記住用戶在Android應用程序中所做的登錄。我需要的應用程序來記住一些數據的喜歡的用戶名和個人資料照片,但現在的應用程序,當我重新啓動(關閉並重新開啓),丟失數據的喜歡的用戶名和個人資料圖片應用使應用程序在退出應用程序後記住Facebook登錄會話

public class SelfTrail extends AppCompatActivity { 

private LoginButton btnLogin; 
private TextView facebookName; 
private CallbackManager callbackManager; 
private ProfilePictureView profilePictureView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 

    setContentView(R.layout.activity_self_trail); 

    btnLogin = (LoginButton)findViewById(R.id.login_button); 
    facebookName = (TextView)findViewById(R.id.name); 
    profilePictureView = (ProfilePictureView)findViewById(R.id.image); 


    btnLogin.setReadPermissions(Arrays.asList("public_profile, email")); 
    callbackManager = CallbackManager.Factory.create(); 
    btnLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 

     @Override 
     public void onSuccess(LoginResult loginResult) { 
      GraphRequest request = GraphRequest.newMeRequest(
        loginResult.getAccessToken(), 
        new GraphRequest.GraphJSONObjectCallback() { 

         @Override 
         public void onCompleted(JSONObject object, GraphResponse response) { 
          Log.v("Main", response.toString()); 
          setProfileToView(object); 
         } 
        }); 
      Bundle parameters = new Bundle(); 
      parameters.putString("fields", "id,name"); 
      request.setParameters(parameters); 
      request.executeAsync(); 
     } 

     @Override 
     public void onCancel() { 

     } 

     @Override 
     public void onError(FacebookException exception) { 

     } 
    }); 
    } 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    callbackManager.onActivityResult(requestCode, resultCode, data); 
} 

private void setProfileToView(JSONObject jsonObject) { 
    try { 
     facebookName.setText(jsonObject.getString("name")); 

     profilePictureView.setPresetSize(ProfilePictureView.NORMAL); 
     profilePictureView.setProfileId(jsonObject.getString("id")); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
}} 

隨時糾正代碼,謝謝

+0

您是否嘗試使用偏好設置,或者您也可以使用onSaveInstanceState。只要看看它是否適合你。 –

+0

@AmitKumar我是那麼沮喪的人我不在哪裏,如何實施,一直在嘗試,因爲你說 – Eggsy

+0

http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please - 你試試有些像onsaveinstancestate的答案中的這樣? –

回答

0

我建議你在本地保存個人資料圖片,並記住該圖片的位置字符串以及用戶名和密碼。這是一個簡單的存儲類我開始與存儲像用戶名名和密碼信息

public class UserLocalStore { 
    public int x = 0; 
    public static final String SP_NAME = "userDetails"; 
    SharedPreferences userLocalDatabase; 
    public int currentID; 
    public UserLocalStore(Context context){ 
     userLocalDatabase = context.getSharedPreferences(SP_NAME, 0); 

    } 
    public void storeUserData(User user){ 
     SharedPreferences.Editor spEditor = userLocalDatabase.edit(); 
     //spEditor.putString("ID", Integer.toString(x)); 
     spEditor.putString(Integer.toString(x) +" name", user.name); 
     spEditor.putString(Integer.toString(x) +" username", user.username); 
     spEditor.putString(Integer.toString(x) +" password", user.password); 
     x++; 
     spEditor.commit(); 

    } 
    public int getByUsername(String findusername){ 
     int x = 0; 

     while(x < 10){ 
      String test = userLocalDatabase.getString(Integer.toString(x)+ " username", ""); 
     if(test.equals(findusername)){ 

      String name = userLocalDatabase.getString(Integer.toString(x)+" name", ""); 
      String username = userLocalDatabase.getString(Integer.toString(x)+" username", ""); 
      String password = userLocalDatabase.getString(Integer.toString(x)+" password", ""); 
      User foundUser = new User(name,username,password); 
      return x; 
     } 
      x++; 
     } 


     return 0; 
    } 

    public ArrayList<User> getAllUsers(){ 
     int x = 0; 
     ArrayList<User> allUsers = new ArrayList<>(); 
     User tempuser = new User("","",""); 
     while(x < 10) { 
      if(userLocalDatabase.getString(Integer.toString(x) + " name", "").equals("")) { 
      return allUsers; 
      } 
      String name = userLocalDatabase.getString(Integer.toString(x) + " name", ""); 
      String username = userLocalDatabase.getString(Integer.toString(x) + " username", ""); 
      String password = userLocalDatabase.getString(Integer.toString(x) + " password", ""); 
      tempuser.name = name; 
      tempuser.username = username; 
      tempuser.password = password; 
      allUsers.add(tempuser); 
      x++; 
     } return allUsers; 

    } 

    public User getLoggedInUser(int ID){ 
     String name = userLocalDatabase.getString(Integer.toString(ID)+" name", ""); 
     String username = userLocalDatabase.getString(Integer.toString(ID)+" username", ""); 
     String password = userLocalDatabase.getString(Integer.toString(ID)+" password", ""); 

     User storedUser = new User(name,username,password); 
     return storedUser; 
    } 
    public User getStoredPreferences(){ 
     String name = userLocalDatabase.getString("name", ""); 
     String username = userLocalDatabase.getString("username", ""); 
     String password = userLocalDatabase.getString("password", ""); 

     User storedUser = new User(name,username,password); 
     return storedUser; 
    } 


    public void setUserLoggedIn(int loggedIn){ 
     currentID = loggedIn; 
     } 

    public int getUserLoggedIn() { 
     return currentID; 
    } 

    public void clearUserData(){ 
     SharedPreferences.Editor spEditor = userLocalDatabase.edit(); 
     spEditor.clear(); 
     spEditor.commit(); 


    } } 

然後你可以從另一個類調用它

User addthisuser = new User(name, username, password); 

userLocalStore.storeUserData(addthisuser);

相關問題