用于android登录活动的数据库

时间:2015-10-21 09:59:20

标签: android login

我是Android开发的新手,我正在使用登录活动。因此,我需要创建一个中央数据库,以便所有用户都可以进行身份​​验证并登录。所以请告诉我如何才能这样做。 我可以使用sqlite创建此类数据,还是仅用于为每个客户端创建本地数据库? 谢谢

7 个答案:

答案 0 :(得分:2)

要在应用程序中允许多个用户登录,您需要使用中央数据库。

以下链接对您有很大帮助。

  

http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

答案 1 :(得分:0)

如果您希望API跨越多个用户,则需要API才能执行此操作。我推荐使用Parse作为初学者,因为它易于设置和使用。

https://www.parse.com/

教程:

http://www.androidbegin.com/tutorial/android-parse-com-simple-login-and-signup-tutorial/

答案 2 :(得分:0)

我从架构的角度回答了类似问题here

答案 3 :(得分:0)

使用SQL数据库而不是sqlite,并使用剪切首选项进行本地存储。这是在任何应用程序中创建登录活动的最佳选择。

答案 4 :(得分:0)

案例1:

如果服务器中有中央数据库,则只需使用 SharedPreferences ,用于保存从Server获取的个人客户端设备的登录凭据。无需为它创建数据库。

情况2:

如果您想在移动设备中设置中央数据库,请使用 Sqlite 保存登录凭据。

答案 5 :(得分:0)

对于剪切prefrence创建一个类名Session?Manager

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);
    }


}

并在LoginActivity类中调用它...

session = new SessionManager(getApplicationContext());
        SharedPreferences pref = session.pref;

答案 6 :(得分:0)

请查看使用PHP with MySQL.

对于自动登录,我建议您保留会话,而不是保留用户ID和密码。