get Primary Email Account of android phone

谁说胖子不能爱 提交于 2019-12-04 00:26:24

As far as I read, there is no concept of primary email id in android. and there is no way to get e-mail id associated with play store. so what I did is, I have fetched all gmail ids and took the last one, it is not the main email id, but it should be the first added google account in his device. so in normal use cases user won't play with his first added email id. so we can assume it as primary mail id.

Mohammed Nathar

I agree with @driodev. but I have done it with a different approach. just copy and paste the code... I might be answering this question a bit late but I guarantee this will be helpful to many in future. In fact, I think we can get any account id that is used in the android device just by changing the string in getAccount("com.example"). Here is the code.

String User_EmailId = getEmiailID(getApplicationContext());

 private String getEmailID(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account account = getAccount(accountManager);
    if (account == null) {
        return null;
    } else {
        return account.name;
    }
}

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;
}    

`

The account you have "associated with the Play Store" is just an app preference of the Play app. You can't read that. The user could download/purchase some apps with test and some with test_new.

Some additions to @MohammedNathar's answer. On Android 6.0 and above, don't forget to request permissions, because this versions are some paranoic:

Manifest:

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

Request:

private void requestPermissions(Activity activity) {
    if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.GET_ACCOUNTS}, 101);
        return null;
    }
}

And in activity

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    boolean result = false;
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        result = true;
    switch (requestCode) {
        case 101:
            if (result) Log.d(Constants.LOG, "Permission GET_ACCOUNTS granted");
            break;
    }
}

Or you can do it with one "if" if you want.

Whenever user assign email_id then android create a calendar with Email.So if above solution not turning out for you then you can try this as hack. Find Email associated with Calendar. Hope this will help.

    public String getCalendarIdAndEmail(Context c) {

    String projection[] = {"_id", "calendar_displayName"};
    //   Uri calendars = Uri.parse("content://com.android.calendar/calendars");
    String calID = null;
    try {
        ContentResolver contentResolver = c.getContentResolver();

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
            return calID;
        }
        Cursor managedCursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1 AND " + CalendarContract.Calendars.IS_PRIMARY + "=1", null, CalendarContract.Calendars._ID + " ASC");
        if (managedCursor.getCount() <= 0) {
            managedCursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1", null, CalendarContract.Calendars._ID + " ASC");
        } else {
            Log.d("getCount", "" + managedCursor.getCount());
        }

        if (managedCursor.moveToFirst()) {

            int nameCol = managedCursor.getColumnIndex(projection[1]);
            int idCol = managedCursor.getColumnIndex(projection[0]);
            do {
               String calName = managedCursor.getString(nameCol);
                calID = managedCursor.getString(idCol);
                //CalName is Email id you are looking for
                Log.e("tag", "calName " + calName + "____calId " + calID);

            } while (managedCursor.hasNext());//managedCursor.moveToNext());
            managedCursor.close();
        }
    } catch (Exception e) {
        Log.e("error", e.getMessage(););
    }
    return calID;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!