在android中欢迎用户使用他们的用户名

时间:2014-08-07 12:35:19

标签: android-intent

我正在Android上构建一个应用程序,用户必须登录才能访问该应用程序。 它连接到远程服务器。我还想欢迎用户在成功登录“welcome + username”后被引导到新活动,我知道该过程必须在意图部分完成,但我使用的是switch语句。这是我的完整代码。

public class LoginActivity extends Activity实现OnClickListener {

private EditText user, pass;
private Button mSubmit, mRegister;

// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

// php login script location:

// localhost :
// testing on your device
// put your local ip instead, on windows, run CMD > ipconfig
// or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL =
// "http://xxx.xxx.x.x:1234/webservice/login.php";

// testing on Emulator:
//private static final String LOGIN_URL = "http://10.0.2.2:1234/webservice/login.php";

// testing from a real server:
private static final String LOGIN_URL = "http://10.0.2.2:1234/webservices/login.php";
// "http://10.0.2.2:1234/webservice/login.php";



// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);


    final ActionBar actionBar = getActionBar();
    /*actionBar.hide();*/
    actionBar.setCustomView(R.layout.actionbar_login);
     actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
    /*actionBar.setBackgroundDrawable(getResources().getDrawable((R.drawable.actionbar)));*/

    // setup input fields
    user = (EditText) findViewById(R.id.username_login);
    pass = (EditText) findViewById(R.id.password_login);

    // setup buttons
    mSubmit = (Button) findViewById(R.id.loginBtn);
    mRegister = (Button) findViewById(R.id.register);

    // register listeners
    mSubmit.setOnClickListener(this);
    mRegister.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.loginBtn:
        new AttemptLogin().execute();

        break;
    case R.id.register:
        Intent i = new Intent(this, SignUpActivity.class);
        startActivity(i);
        break;

    default:
        break;
    }
}



class AttemptLogin extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
        String username = user.getText().toString();
        String password = pass.getText().toString();
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("password", password));

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                    params);

            // check your log for json response
            Log.d("Login attempt", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.d("Login Successful!", json.toString());
                // save user data
                SharedPreferences sp = PreferenceManager
                        .getDefaultSharedPreferences(LoginActivity.this);
                Editor edit = sp.edit();
                edit.putString("username", username);
                edit.commit();

                Intent i = new Intent(LoginActivity.this, StateActivity.class);
                finish();
                startActivity(i);
                return json.getString(TAG_MESSAGE);
            } else {
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(LoginActivity.this, file_url, Toast.LENGTH_LONG).show();
        }




    }

}

}

1 个答案:

答案 0 :(得分:1)

首先,这看起来不是正确的地方,因为我认为在用户成功通过身份验证时会调用代码中的某种回调? (我从你发布的代码中看不到这一点。)

然而......当你达到了解用户已成功通过认证的程度时,你可以使用多种方法将他们的用户名传递给你想要向他们打招呼的活动......

在意图

上使用捆绑包
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString("username", value);

在意图上使用PutExtra

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra("username", value);

制作新的意图

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString("username", value);
mIntent.putExtras(mBundle);

然后在活动中你会这样做再次检索用户名......

String username = getIntent().getExtras().getString("username");

希望有所帮助。