Skip to content

Instantly share code, notes, and snippets.

@msomu
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save msomu/3d6273eb90b3eacc493d to your computer and use it in GitHub Desktop.

Select an option

Save msomu/3d6273eb90b3eacc493d to your computer and use it in GitHub Desktop.
Get the user Email id without even saying the user.
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
public class SplashActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String user_mail = "NoMail";
user_mail = UserEmailFetcher.getEmail(MainActivity.this);
Log.e("SOMU",user_mail);
}
}
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
/**
* Created by msomu on 19/9/14.
* This class is used for getting the Registered E-Mail id on Google Play account.
*/
public class UserEmailFetcher {
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment