3

这是一个用户凭据检查的模拟,大部分代码是由 Android Studio 生成的。如果用户:通行证不存在,它会进行注册,但我不确定我应该将用户发送到主要活动的位置。请看一下checking mail & pass部分。

/**
 * Represents an asynchronous login/registration task used to authenticate
 * the user.
 */
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO: attempt authentication against a network service.

        try {
            // Simulate network access.
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            return false;
        }
 // checking mail & pass
        for (String credential : DUMMY_CREDENTIALS) {
            String[] pieces = credential.split(":");
            if (pieces[0].equals(mEmail)) {
                // Account exists, return true if the password matches.
                return pieces[1].equals(mPassword);

            }

        }

        // TODO: register the new account here.
        Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(i);
        return true;
    }

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

        if (success) {
            finish();
        } else {
            mPasswordView.setError(getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
        }
    }

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

}

在 user:pass 正确后,我在哪里开始主要活动?

4

3 回答 3

3

我现在找到了。里面if (success)

于 2013-09-03T02:57:35.650 回答
2

试试这个..这肯定会奏效..

/** * 表示用于对用户进行身份验证的异步登录/注册任务。*/

public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO: attempt authentication against a network service.

        try {
            // Simulate network access.
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            return false;
        }
 // checking mail & pass
        for (String credential : DUMMY_CREDENTIALS) {
            String[] pieces = credential.split(":");
            if (pieces[0].equals(mEmail)) {
                // Account exists, return true if the password matches.
                return pieces[1].equals(mPassword);

            }

        }
        return false;
    }

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

        if (success) {
            Intent i = new Intent(LoginActivity.this, MainHomePageActivity.class);
            startActivity(i);
        } else {
            Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(i);
        }
    }

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

}
于 2013-09-03T07:54:16.850 回答
1
/**
 * Represents an asynchronous login/registration task used to authenticate
 * the user.
 */
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
    // TODO: attempt authentication against a network service.

    try {
        // Simulate network access.
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        return false;
    }
// checking mail & pass
    for (String credential : DUMMY_CREDENTIALS) {
        String[] pieces = credential.split(":");
        if (pieces[0].equals(mEmail)) {
            // Account exists, return true if the password matches.
            return pieces[1].equals(mPassword);

        }

    }

    return false;
}

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

    if (success) {
    // TODO: register the new account here.
    Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
    startActivity(i);
    finish();
    } else {
        mPasswordView.setError(getString(R.string.error_incorrect_password));
        mPasswordView.requestFocus();
    }
}

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

}
于 2013-09-03T03:26:37.493 回答