3
对于我正在开发的android应用程序,它需要创建一个配置文件来发送和接收其他用户的内容。对于登录过程,用户是否可以使用手机上关联的Gmail帐户登录?这将是相同的帐户,积极使用谷歌播放。使用Gmail帐户登录我的应用程序?
这将使整个登录过程非常流畅,我认为这将是最好的情况。
非常感谢!
对于我正在开发的android应用程序,它需要创建一个配置文件来发送和接收其他用户的内容。对于登录过程,用户是否可以使用手机上关联的Gmail帐户登录?这将是相同的帐户,积极使用谷歌播放。使用Gmail帐户登录我的应用程序?
这将使整个登录过程非常流畅,我认为这将是最好的情况。
非常感谢!
是的,这是可能的,它很简单。在开始之前,你需要添加,一个允许你的AndroidManifest.xml
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
它可以读取与设备关联的账户。然后你在应用程序内执行以下操作:
Account[] accounts = AccountManager.get(context).getAccountsByType("com.google");
这将返回用户的所有Google帐户。在你的情况,也许你只需要电子邮件,所以这里是一个快速静态函数,这将使他们给你:
public static String[] getAccounts(Context context) {
Account[] accounts = AccountManager.get(context).getAccountsByType("com.google");
String[] names = new String[accounts.length];
for (int i = 0; i < accounts.length; ++i) names[i] = accounts[i].name;
return names;
}
该函数将返回设备上的所有Gmail电子邮件(谷歌帐户)的字符串数组。
但是,如果您需要与某些Google服务进行“交谈”以获取更多信息,则必须执行以下操作。第一个多个权限添加到清单:
<uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>
此权限将允许您的应用程序使用使用证书来识别用户在谷歌的一些服务的前面。它不会给你的凭据(密码)。
要使用一些Google服务,您需要一个令牌。这是一个快速的功能:
public static String getToken(Activity activity, String serviceName) {
try {
Bundle result = AccountManager.get(activity).getAuthToken(account, serviceName, null, activity, null, null).getResult();
return result.getString(AccountManager.KEY_AUTHTOKEN);
} catch (OperationCanceledException e) {
Log.d("Test", "Operation Canceled");
} catch (AuthenticatorException e) {
Log.d("Test", "Authenticator Exception");
} catch (IOException e) {
Log.d("Test", "Auth IOException");
}
return null;
}
一旦你的令牌,你只是HTTP API他们已经和大家玩的开心:-)
退房这个职位,这可能是你所需要的: http://stackoverflow.com/questions/3360926/get-main-gmail-account-username-in-android-2-0,或可能是这一个:http://stackoverflow.com/questions/2112965/how-to -get最Android的设备 - 主电子邮件地址 –