如何检查是否在Android应用程序中首先登录

时间:2018-01-31 20:01:00

标签: android loopback

我正在开发Android应用程序,通常当我们使用不同的应用程序时,他们只需要一次用户登录,当我们下次使用时,它不需要再次登录,就像Facebook,gmail等

我也希望有这样的功能,所以它应该在共享首选项中保存用户名和密码,我使用强大的环回API(强环回是基本要求)我刚刚编写代码,但我想使用Access Token,可以给我一个如何在android中使用循环访问令牌的想法...... ???

 public boolean checkLogin() {
    final SharedPreferences prefs = this.getSharedPreferences(AndroidGPSTrackingActivity.class.getSimpleName(),
            this.MODE_PRIVATE);
    String userName = prefs.getString("userName", "");
    if (userName.isEmpty()) {
        Log.i("UserName", "UserName not found.");
        return false;
    }

    String password = prefs.getString("password", "");
    if (password.isEmpty()) {
        Log.i("Password", "Password not found.");
        return false;
    }


    mCheckTask = new CheckSavedLogin(userName, password, this);
    mCheckTask.execute((Void) null);


    return mCheckTask.execute((Void) null);//make this return true/false
}

还有:

 public class CheckSavedLogin extends AsyncTask<Void, Void, Boolean> {

    public final String mUserName;
    public final String mPassword;
    public boolean success;
    public Context context;

    CheckSavedLogin(String userName, String password, Context context) {
        mUserName = userName;
        mPassword = password;
        this.context = context.getApplicationContext();
    }

    protected Boolean doInBackground(Void... params) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://173.242.94.66/scripts/appLogin.php");

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("userName", mUserName));
            nameValuePairs.add(new BasicNameValuePair("password", mPassword));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            String responseStr = EntityUtils.toString(response.getEntity());

            JSONObject json = new JSONObject(responseStr);
            success = json.getBoolean("success");

            Log.d("Response", responseStr);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return success;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
        showProgress(false);

        if (success) {
            storeLoginDetails(mUserName, mPassword);
            //RETURN TRUE HERE?
        }
    }

    @Override
    protected void onCancelled() {
        mAuthTask = null;
        showProgress(false);
    }
}

0 个答案:

没有答案