[android]保存登录信息,如果应用程序崩溃

发布时间: 2016/6/29 19:43:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个登录屏幕,电子邮件和密码字段。一旦用户通过验证,访问令牌是给用户和适当地更新。

但是,当应用程序崩溃并重新启动,该令牌无效因为电子邮件和密码都将丢失。

如何可以恢复电子邮件和密码在 onResume()?

     private class UserLoginTask extends AsyncTask<Void, Void, String> {

        private final String lurl;
        private final String mEmail;
        private final String mPassword;

        UserLoginTask(String url, String email, String password) {
            lurl = url;
            mEmail = email;
            mPassword = password;
        }

        @Override
        protected String doInBackground(Void... params) {


            try {
                WebConnection conn = new WebConnection(lurl, getApplicationContext());
                conn.addValuePair("username", mEmail);
                Log.v("mEmail", mEmail);
                conn.addValuePair("password", mPassword);
                Log.v("mPassword", mPassword);

                String response = conn.connect();


                return response;


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


            return null;

}

        @Override
        protected void onPostExecute(final String response) {
            mAuthTask = null;
            showProgress(false);


            if(response != null) {

                SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
                editor.putString("email", mEmail);
                editor.putString("password", mPassword);
                editor.apply();
            }

//NEED TO GET RESPONSE FROM SERVER IF LOGIN BASED ON PREFS WAS SUCCESSFUL
            if (success) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            } else {
                mPasswordView.setError(getString(R.string.error_incorrect_password));
                mPasswordView.requestFocus();
            }
        }





     @Override
        protected void onResume() {
            super.onResume();


            Context context = getApplicationContext();
            SharedPreferences prefs;
            prefs = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
            String token = prefs.getString("token", null);

            //NEED TO GET mEmail and mPassword values from other activity
            if (mEmail == null) //Check if it isn't already set
                mEmail = prefs.getString("email", null);
            if (mPassword == null)
                mPassword = prefs.getString("password", null);
if(token == null || token.isEmpty()){
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }

    }

解决方法 1:

把它存储在 SharedPreferences 中你 onPostExecute:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("email", mEmail);
editor.putString("password", mPassword);
editor.apply();

读它在你的 onResume:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
if (mEmail == null) //Check if it isn't already set
  mEmail = prefs.getString("email", null);
if (mPassword == null)
  mPassword = prefs.getString("password", null);

//Do your stuff here, but check if the Strings are null before

更好的办法,当然是摆脱崩溃。它不是一个好兆头,如果您确信您的应用程序将会崩溃,尤其是在第一个屏幕上。

官方微信
官方QQ群
31647020