2015-11-02 35 views
1

我允許註冊用戶訪問該應用程序。用戶每次打開應用程序時都必須登錄。所以我想知道如何一次性登錄應用程序,如Facebook或WhatsApp?如何在android應用程序中登錄一次?

+0

使用剪切偏好爲此目的。 –

+0

是的共享偏好 –

+0

我想這實際上取決於你如何實現你的後端,你可以給我們更多的細節,如何以及你用什麼來實現登錄? – TPWang

回答

1

您可以使用共享偏好

Android提供了許多存儲應用程序數據的方法。其中一種方式稱爲Shared Preferences。共享首選項允許您以key,value pair的形式保存和檢索數據。

SO Help

  • 您可以從Shared 首選項中保存和檢索關鍵值對數據。 SharedPreferences值將在用戶 會話中持續存在。
  • 即使用戶關閉了 應用程序,共享首選項中的數據仍將保留。
  • 您可以使用 getSharedPreferences()方法從共享首選項中獲取值。

  • 您還需要編輯器來編輯和保存共享 首選項中的更改。

  • 使用SharedPreferences存儲數據:布爾值,浮點數,整數,長整數, 和字符串。

演示

  1. Android User Session Management using Shared Preferences
  2. Android Working with Shared Preferences
0

您應該使用SharedPreferences來存儲成功的登錄操作。

保存狀態共享偏好

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
editor.putString("username", "yourUserName"); 
editor.putBoolean("isLoggedIn", true); 
editor.commit(); 

從SharedPreference

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
boolean isLoggedIn = prefs.getBoolean("isLoggedIn", false); 
if (isLoggedIn) { 
     // Don't display login activity 
} else { 
     // Display Login Activity 
} 
檢索狀態
0
public class SessionManager { 
    // Email address (make variable public to access from outside) 
    public static final String KEY_api = "apikey"; 
    private static final String KEY_NAME = "name"; 
    // Sharedpref file name 
    private static final String PREF_NAME = "AndroidHivePref"; 
    private static final String push_ = "AndroidHivePref"; 
    // All Shared Preferences Keys 
    private static final String IS_LOGIN = "IsLoggedIn"; 
    // Shared Preferences 
    SharedPreferences pref; 
    // Editor for Shared preferences 
    SharedPreferences.Editor editor; 
    // Context 
    Context _context; 
    // Shared pref mode 
    int PRIVATE_MODE = 0; 


    // Constructor 
    public SessionManager(Context context) { 
     this._context = context; 
     pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); 
     editor = pref.edit(); 
    } 

    public void createLoginSession(String name, String apikey) { 
     // 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_api, apikey); 


     // commit changes 
     editor.commit(); 
    } 

    public int getMerchentPushCount() { 
     return pref.getInt("MerchentPushCount", 0); 
    } 

    public void setMerchentPushCount(Integer count) { 
     editor.putInt("MerchentPushCount", count); 

     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, LoginActivity.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.put(KEY_NAME, pref.getString(KEY_NAME, null)); 
     // user api key 
     user.put(KEY_api, pref.getString(KEY_api, 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, LoginActivity.class); 
     // Closing all the Activities 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 

       Intent.FLAG_ACTIVITY_CLEAR_TASK | 
       Intent.FLAG_ACTIVITY_NEW_TASK); 
     i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 

     // Staring Login Activity 
     _context.startActivity(i); 
    } 

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


} 

創建剪切prefrence一類,並調用它在烏爾活動其中U想使作爲登陸頁面。參考以下this教程

相關問題