I want to build application to get email and user IP when he open my app on Android, but I can't.
Can you write some code for me?
Thanks!
- 3
- 3
-
1[e-mail](http://stackoverflow.com/questions/2112965/how-to-get-the-android-devices-primary-e-mail-address) and [ip](http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device). Next time, please try searching the stuff before asking, it's probably has been asked before. – Balázs Édes Jun 02 '14 at 20:41
2 Answers
after some search and viewing other posts on stack i came to know that
The account manager class has access to get email.
http://developer.android.com/reference/android/accounts/AccountManager.html
AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();
String gmail = null;
for(Account account: list)
{
if(account.type.equalsIgnoreCase("com.google"))
{
gmail = account.name;
break;
}
}
And you will need the following permission in your manifest:
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
and the above stuff works only for >2.0 and if u want to use below that you can go through the other posts of stack
Get main gmail account username in Android < 2.0
and here are some other links also that may help you
How to get the Android device's primary e-mail address
Using google-account to log in on Android Application
and for ip address you must go through these references which had given very suggestible answers
How to get IP address of the device from code?
How to get User IP address surfing internet on my Android App through his phone/tablet
Get MAC Address of android device without Wifi
How to find MAC address of an Android device programmatically
- 1
- 1
- 3,281
- 1
- 12
- 14
To Get IP Address:
WifiManager _wifi = (WifiManager) getSystemService(WIFI_SERVICE);
String _ipAddress = Formatter.formatIpAddress(_wifi .getConnectionInfo().getIpAddress());
Add below permission in the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
To Get EMAIL Id, use Account Manager
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 _emailId= account.name;
}
}
Add below permission in the manifest file:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
- 11,041
- 2
- 38
- 62