我想构建应用程序,以便在Android上打开我的应用程序时获取电子邮件和用户IP,但我无法做到。 你能为我写一些代码吗? 谢谢!
答案 0 :(得分:1)
经过一些搜索和查看堆栈上的其他帖子后我才知道
帐户经理类可以访问电子邮件。
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;
}
}
您需要在清单中获得以下权限:
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
以上内容仅适用于&gt; 2.0,如果你想在下面使用,你可以浏览堆栈的其他帖子
Get main gmail account username in Android < 2.0
以及其他一些可能对您有帮助的链接
How to get the Android device's primary e-mail address
Using google-account to log in on Android Application
对于ip地址,你必须通过这些提供非常明确的答案的引用
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)
WifiManager _wifi = (WifiManager) getSystemService(WIFI_SERVICE);
String _ipAddress = Formatter.formatIpAddress(_wifi .getConnectionInfo().getIpAddress());
在清单文件中添加以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
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;
}
}
在清单文件中添加以下权限:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />