I am trying to get primary Hmail id from the device automatically, but its only working in the 5.0 and below version, not working in the 6.0 and above versions.

My code is:

```
private TextView text;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.text);
    String email=getEmail(this);
    if(email !=null)
    {
        text.setText(email);
    }
    else {
        text.setText("Account not found");
    }
}

static String getEmail(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;
}

```