我希望允许用户在不输入电子邮件地址的情况下向我提供他们的电子邮件地址.理想情况下,应该有一个文本字段,用户可以在其中输入电子邮件地址或按下按钮来自动填充它.
I want to allow the user to provide me their email address without typing it in. Ideally, there'd be a text field where the user could either type an email address or push a button to autofill it.
在较早的问题中,Roman Nurik 建议使用一个 AccountManager 来处理这个,但这需要我的应用程序使用 GET_ACCOUNTS 权限;然后,我的应用程序可以访问设备上所有用户的帐户,包括他们的 Facebook/Twitter 帐户.这个权限似乎对我想要的来说太宽泛了.
In an earlier question, Roman Nurik suggests using an AccountManager to handle this, but that requires my app to use the GET_ACCOUNTS privilege; my app could then access all of the user's accounts on the device, including their Facebook/Twitter accounts. That permission seems way too broad for what I want.
有没有更好的方法来处理这个问题,而不需要授予我的应用如此重的权限?
Is there a nicer way to handle this that doesn't require granting my app such a heavy duty permission?
推荐答案我知道我来得太晚了,但这可能对其他人有用.
I know I'm way too late, but this might be useful to others.
我认为现在自动填充电子邮件字段的最佳方法是使用 帐户选择器
I think the best way to auto-populate an email field now is by using AccountPicker
如果您的应用具有 GET_ACCOUNTS 权限并且只有一个帐户,您可以立即获得它.如果您的应用没有它,或者有多个帐户,用户会收到提示,以便他们可以授权或不授权该操作.
If your app has the GET_ACCOUNTS permission and there's only one account, you get it right away. If your app doesn't have it, or if there are more than one account, users get a prompt so they can authorize or not the action.
您的应用需要包含 Google Play Services 身份验证库com.google.android.gms:play-services-auth 但它不需要任何权限.
Your app needs to include the Google Play Services auth library com.google.android.gms:play-services-auth but it doesn't need any permissions.
这整个过程在旧版本的 Android 上会失败(需要 2.2+),或者如果 Google Play 不可用,那么你应该考虑这种情况.
This whole process will fail on older versions of Android (2.2+ is required), or if Google Play is not available so you should consider that case.
这是一个基本的代码示例:
Here's a basic code sample:
private static final int REQUEST_CODE_EMAIL = 1; private TextView email = (TextView) findViewById(R.id.email); // ... try { Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null); startActivityForResult(intent, REQUEST_CODE_EMAIL); } catch (ActivityNotFoundException e) { // TODO } // ... @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) { String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); email.setText(accountName); } }更多推荐
很好地获取 Android 所有者的电子邮件地址
发布评论