如何检测用户取消AccountPicker对话框Android Eclipse

时间:2014-07-19 20:22:19

标签: java android eclipse accountpicker

我想通过 AccountPicker.newChooseAccountIntent 来获取用户电子邮件ID。我想要检测用户取消AccountPicker对话框

这是一个代码

     private static final int REQUEST_CODE_EMAIL = 1;
     private TextView email = (TextView) findViewById(R.id.email);



     try {
         Intent intent = AccountPicker.newChooseAccountIntent(null, null,
            new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
         startActivityForResult(intent, REQUEST_CODE_EMAIL);
     } catch (ActivityNotFoundException e) {
         // TODO
     }



     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            email.setText(accountName);
       }
    }

1 个答案:

答案 0 :(得分:3)

活动中还有RESULT_CANCELEDRESULT_CANCEL常量。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
       String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
       email.setText(accountName);
    } else if(requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_CANCELED) {
      Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
    }  
}