How to obtain a GMAIL account already registered in the device?

1

What I want is that, instead of entering the email and password ( "[email protected]", "xxxxpassword" ), directly in the code, use the email already registered by the user on the phone , I hope your help thanks.

@Override
protected String doInBackground(Void... params) {

    try {
        GMailSender sender = new GMailSender("[email protected]", 
 "xxxxpassword");
        sender.sendMail("This is a testing mail",
                "This is Body of testing 
 mail","[email protected]",
                "[email protected]")                   ;

    } catch (Exception e) {
        Log.e("error", e.getMessage(), e);
        return "Email No Enviado";
    }
    return "Email Enviado";
}

@Override
protected void onPostExecute(String result) {
    Log.e("LongOperation",result+"");
}
@Override
protected void onPreExecute() {

}
@Override
protected void onProgressUpdate(Void... values) {
 }
 }
    
asked by Oskr1024 20.12.2017 в 16:18
source

1 answer

3

To enter the user's email you must first obtain it, but you must remember that a device can have several email accounts configured in it, for example this device has 2 registered accounts:

In this case, if you have more than one account, you can get the main one:

String  emailAccount = "";
        AccountManager accountManager = AccountManager.get(getApplicationContext());
        Account account = getAccount(accountManager);
        if (account != null) {
            emailAccount =  account.name;
        }

this is the getAccount() method:

private static Account getAccount(AccountManager accountManager) {
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
        account = accounts[0];
    } else {
        account = null;
    }
    return account;
}

This way you would get the main account registered in the device.

    
answered by 20.12.2017 в 21:49