无法设置会话

时间:2018-05-10 06:20:46

标签: android

我是android的新手。在我的应用程序中我创建了一个登录页面。我使用共享首选项进行session.it工作正常,但是当我关闭我的应用程序并再次打开然后登录页面出现时出现问题。我想在用户按下时只有当登录页面到来时才会退出按钮。

  

这是我的SharedPreferences类

public class SharedPrefManager {

//the constants
private static final String SHARED_PREF_NAME = "dreamzsharedpref";
private static final String KEY_USERNAME = "keyusername";
private static final String KEY_PHONE = "keyphone";
private static final String KEY_ID = "keyid";

private static SharedPrefManager mInstance;
private static Context mCtx;

private SharedPrefManager(Context context) {
    mCtx = context;
}

public static synchronized SharedPrefManager getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new SharedPrefManager(context);
    }
    return mInstance;
}

//method to let the user login
//this method will store the user data in shared preferences
public void userLogin(User user) {
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(KEY_ID, user.getUserid());
    editor.putString(KEY_PHONE, user.getUserphno());
    editor.putString(KEY_USERNAME, user.getUsername());
    editor.apply();
}

//this method will checker whether user is already logged in or not
public boolean isLoggedIn() {
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_PHONE, null) != null;
}

//this method will give the logged in user
public User getUser() {
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return new User(
            sharedPreferences.getString(KEY_USERNAME, null),
            sharedPreferences.getString(KEY_PHONE, null),
            sharedPreferences.getInt(KEY_ID, -1)
    );
}


//this method will logout the user
public void logout() {
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.apply();
    mCtx.startActivity(new Intent(mCtx, LoginActivity.class));
}
}
  

这是登录类

中的登录方法
private void userLogin() {

    //first getting the values
    final String username = UsernameEt.getText().toString();
    final String password = PasswordEt.getText().toString();

    //validating inputs
    if (TextUtils.isEmpty(username)) {
        UsernameEt.setError("Please enter your username");
        UsernameEt.requestFocus();
        return;
    }

    if (TextUtils.isEmpty(password)) {
        PasswordEt.setError("Please enter your password");
        PasswordEt.requestFocus();
        return;
    }

    //if everything is fine

    class UserLogin extends AsyncTask<Void, Void, String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(Void... voids) {
            //creating request handler object
            RequestHandler requestHandler = new RequestHandler();

            //creating request parameters
            HashMap<String, String> params = new HashMap<>();
            params.put("vuserphno", username);
            params.put("votp", password);

            //returing the response
            return requestHandler.sendPostRequest(URLs.URL_LOGIN, params);//this URLs is a class where URL_LOGIN is login url
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressBar.setVisibility(View.GONE);

            try {
                JSONObject obj = new JSONObject(s);
                JSONArray array = obj.getJSONArray("user");

                for (int i = 0; i < array.length(); i++) {
                    //getting the user from the response
                    JSONObject userJson = array.getJSONObject(i);

                    //creating a new user object
                    User user = new User(
                            userJson.getString("username"),
                            userJson.getString("userphno"),
                            userJson.getInt("userid")
                    );

                    //storing the user in shared preferences
                    SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);
                }

                    //starting the profile activity
                    finish();
                    startActivity(new Intent(getApplicationContext(), MainActivity.class));

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

    UserLogin ul = new UserLogin();
    ul.execute();
}

请告诉我问题出在哪里以及如何解决这个问题。 感谢。

1 个答案:

答案 0 :(得分:0)

['tBTCUSD', 'tLTCUSD', 'tLTCBTC', 'tETHUSD', 'tETHBTC', 'tETCBTC', ...] 中,检查登录状态并更改活动,完成自身,以便从后面的堆栈中清楚显示。

LoginActivity