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

我只是不知道如何使这一切返回"真"或"假"基于响应

通常我会提供一个回调。

public interface LoginHandler
{
    onLoginComplete(boolean didLogIn);
}

然后我将在执行之前在AsyncTask中注册处理程序。

asyncTask.setHandler(new MyLoginHandler());
asyncTask.execute(0);

,然后在调用活动触发时返回,您可以通过处理程序检索结果。

public class MyLoginHandler implements LoginHandler
{
   public void onLoginComplete(boolean didLogIn)
   {
      //Do what you want with didLogIn
   }
}

相关内容

  • 没有找到相关文章

最新更新