3

私はアプリに取り組んでいます。ユーザーがログインを維持することは、ログインが成功した後は常にサーバーに接続されることを意味します(Facebookアプリのように)。これをグーグルで検索しようとしましたが、正しいロジックが見つかりませんでした。多くの Web サイトは、使用することを提案していますSharedPreferenceが、ユーザーのログイン資格情報を保持しています。on SheredPreference は良い考えではなく、サーバーへの接続を維持するための答えを提供しませんでした。私はこの考えにちょっと行き詰まっています。これを実装するにはロジックが必要です。提案やサンプルコードは大歓迎です。

私はアンドロイド初心者です。

4

5 に答える 5

3

ユーザーの資格情報をデバイスに保存することは、設計上適切な方法ではありません。パスワードを保存することもできますがHash、これは優れたアプリケーション設計手法としても否定されています。facebook と google によると、これらの技術の巨人はAuthenticationトークンのログインとログアウトを使用しています。ユーザーがサーバーにログインすると、特定のユーザーのトークンが生成され、デバイスとサーバーに保存されます。次回ユーザーがアプリにアクセスすると、トークンが有効かどうかを確認するリクエストが行われ、有効な場合はアクセスが許可され、そうでない場合はアクセスが許可されます。

このプロセスの基本設計

ここに画像の説明を入力

チュートリアル:

于 2016-09-23T11:50:30.083 に答える
0

これを試してみてください..

sessionManager.java

   package com.example.sachin.splashlogin;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.HashMap;

public class SessionManager {

    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 = "SocialPref";

    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";

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

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

    // 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 email, String userid){
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);

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

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

        // 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, com.example.sachin.splashlogin.Login.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_ID, pref.getString(KEY_ID, null));

        // return user
        return user;
    }

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

        editor.putBoolean(IS_LOGIN, false);
        // After logout redirect user to Loing Activity
        Intent i = new Intent(_context, Login.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);
    }

}

すべての新しい画面で、このコードを貼り付けるだけです..

 SessionManager session;

このコードを onCreate() に貼り付けます

 session = new SessionManager(getApplicationContext());
        HashMap<String, String> user = session.getUserDetails();
        struid = user.get(SessionManager.KEY_NAME);
于 2016-09-23T07:11:30.353 に答える