.
Back to Top

0 Collect Android Device's Primary Email Address through Apps

If you are running a service or a third party app and wish to build an email list, these tips will help you do so. Whether it is for marketing purpose or for an advertising, you can look for an android user's email information smartly.

Every android phone has primary email id associated with the owner of the phone. Giving away email address for any good purpose may not be a big deal for users as far as it is asked in a legitimate way.

Android operating systems above 2.0 support for multiple email address whereas older OS has support for only one email. Using these APIs an android developer can easily pull the email information through apps and use them wisely.

Using AccountManager (API level 5+)

One can use AccountManager.getAccounts or AccountManager.getAccountsByType to get a list of all emails associated with the particular device.

For this app requires GET_ACCOUNTS permission.

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(context).getAccounts();
for (Account account : accounts) {
    if (emailPattern.matcher(account.name).matches()) {
        String possibleEmail = account.name;
        ...
    }
}
code credits and source: stackoverflow discussion board

Using ContactsContact.Profile (API level 14+)

In Google Android ICS, 4.0, one can get user's email address through their profile. Using CursorLoader you can achieve this.

For this app requires READ_PROFILE and READ_CONTACTS permission.

Using Android's (ICS, 4.0) AccountPicker

Your app may need Google Play Services included but not request for any permissions.

The basic code snippets are as follows.

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);
        }
    }
code credits and source: stackoverflow discussion board

Using AccountManager to construct Gmail address with the same username.

You will need below basic snippets

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();

and ask for the permissions

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>

Get Google Username

Using AccountManager.get(this) you can construct email address by collecting user's google username.

public String getUsername() {
    AccountManager manager = AccountManager.get(this);
    Account[] accounts = manager.getAccountsByType("com.google");
    List<String> possibleEmails = new LinkedList<String>();

    for (Account account : accounts) {

        // TODO: Check possibleEmail against an email regex or treat
        // account.name as an email address only for certain account.type
        // values.
        possibleEmails.add(account.name);
    }

    if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {

        String email = possibleEmails.get(0);
        String[] parts = email.split("@");
        if (parts.length > 0 && parts[0] != null)
            return parts[0];
        else
            return null;
    } else
        return null;
}
code credits and source: stackoverflow discussion board

Get username through Gmail ID

accounts = AccountManager.get(this).getAccounts();
    Log.e("", "Size: " + accounts.length);
    for (Account account : accounts) {

        String possibleEmail = account.name;

        String type = account.type;

        if (type.equals("com.google")) {

            strGmail = possibleEmail;

            Log.e("", "Emails: " + strGmail);

            break;
        }
    }

Permissions needed - 

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

code credits and source: stackoverflow discussion board 

Collecting Google User Profile Details 

You can also collect user's account profile details by simply using AccountUtils.java codes, head to gist.github.com article here

Note: All codes and snippets are for tracking and educational purpose only. We are not responsible for any data loss or misuse.
Related Posts Plugin for WordPress, Blogger...

Zergnet