1

我想要通过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);
       }
    }
4

1 回答 1

4

Activity中还有一个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();
    }  
}
于 2014-07-19T20:50:10.787 回答