Search code examples
androidaccountmanager

How to get the First Gmail account detail when we switch ON our phone and register it


I have the following code that is getting all the gmail id's that are synchronized with my phone but i want the Main gmail id that user register the first time. Because all the other gmail accounts(not main) if i want i can delete anytime but to delete the main account we have to done other things too, So that's why i want to get the main account to use into my application.

here's my code i think to add some filter in it, i can do that but can't able to get the thing correctly.

Account[] accounts=AccountManager.get(this).getAccountsByType("com.google");
    for(Account account: accounts)
    {
        String possibleEmail=account.name;
        Log.d("Possible email id's of user", possibleEmail);
    }

i already seen Roman link but didn't able to convert it in right manner. I want to use this main email id for push notification using C2DM from google.


Solution

  • I have an answer, but it was not a correct way to do it. As i am getting all the accounts associated with google(gmail) and as we enter the email id's to be synchronized with gmail a stack is created and on 0th position, I am getting the primary Gmail id that was first enter by the user when he switched ON his phone.

        Account[] accounts=AccountManager.get(this).getAccountsByType("com.google");
        String myEmailid=accounts[0].toString();
        Log.d("My email id that i want", myEmailid);
        for(Account account: accounts)
        {
            String possibleEmail=account.toString();
            Log.d("Possible email id of user", possibleEmail);
    
        }
    

    If someone has a better solution then let me know, i tested this example on 2 phones and it works fine, but i see many applications that are fetching the primary email id of users to send email and all, i want to know how they are doing?