How To Detect User Cancel AccountPicker dialog Android Eclipse

748 Views Asked by At

i want user Email id via AccountPicker.newChooseAccountIntent. i want Detect User Cancel AccountPicker dialog

here is a code

     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

There are 1 best solutions below

0
On BEST ANSWER

There is also a RESULT_CANCELED or RESULT_CANCEL constant in an Activity.

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