Android M电子邮件填写 [英] Android M email completition

查看:136
本文介绍了Android M电子邮件填写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道,Android M的权限系统已更新.

as you know the permission system on Android M has been updated.

我当前使用GET_ACCOUNTS权限在用户登录/注册我的应用程序时自动完成用户电子邮件.

I currently use the permission GET_ACCOUNTS to autocomplete the user email when he sign in/sign up on my app.

final ArrayList<String> emails = new ArrayList<String>();
for (Account account : AccountManager.get(this).getAccounts()) {
    emails.add(account.name);
}
email.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, emails));

问题在于,在Android M上,要继续使用此功能,我需要向用户询问读取其帐户的权限.这没有任何意义,因为要节省用户一些时间,我需要使用一个令人讨厌的权限请求.

The problem is that on Android M, to continue to use this feature, I'll need to ask the user for the permission to read his accounts. That doesn't make sense because to save the user a little amount of time I need to use an annoying permission request.

还有另一种方法可以自动填写用户电子邮件,而无需征求任何许可?

There's another way to autocomplete the user email without asking for any permission?

推荐答案

Google Play服务8.3添加了提示信息,可用于自动填充电子邮件地址

Google Play Services 8.3 has added hint information which can be used to autofill email addresses

http://android-developers.blogspot.co.uk/2015/11/whats-new-in-google-play-services-83.html

为使跨设备登录变得更容易(无论您使用Google登录还是仍使用基于密码的身份验证),Smart Lock API进行了一些重要更新.我们添加了新的API方法来显示一个对话框,该对话框可帮助您的用户选择以前使用的电子邮件地址来轻松地预先填写登录或注册表单:请查看getHintPicker.不需要任何设备权限,它可以替代您以前从设备上的帐户中填充的选择器,现在需要棉花糖的运行时权限.

And to make signing in easier across devices, whether you use Google Sign-In or still have password-based authentication, the Smart Lock APIs received some important updates. We’ve added a new API method to show a dialog that helps your user select a previously-used email address to pre-fill sign in or up forms easily: check out getHintPicker. This doesn’t require any device permissions and provides an alternative to a picker you may have previously populated from accounts on the device, which would now require a runtime permission with Marshmallow.

https://developers.google.com/identity/smartlock-密码/android/retrieve-提示

HintRequest hintRequest = new HintRequest.Builder()
    .setHintPickerConfig(new CredentialPickerConfig.Builder()
            .setShowCancelButton(true)
            .build())
    .setEmailAddressIdentifierSupported(true)
    .build();

PendingIntent intent =
        Auth.CredentialsApi.getHintPickerIntent(mCredentialsClient, hintRequest);
try {
    startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
    Log.e(TAG, "Could not start hint picker Intent", e);
}

提示用户选择要使用的电子邮件地址.

The user is prompted to choose an email address to use.

然后,在活动的onActivityResult()方法中,从Credential.EXTRA_KEY包中检索提示,检查用户是否在用户数据库中,并使用凭证提示开始适当的活动.

Then, in the activity's onActivityResult() method, retrieve the hints from the Credential.EXTRA_KEY parcel, check whether the user is in your user database, and start the appropriate activity with the credentials hint.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_HINT) {
        if (resultCode == RESULT_OK) {
            Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
            Intent intent;
            // Check for the user ID in your user database.
            if (userDatabaseContains(credential.getId())) {
                intent = new Intent(this, SignInActivity.class);
            } else {
                intent = new Intent(this, SignUpNewUserActivity.class);
            }
            intent.putExtra("com.mycompany.myapp.SIGNIN_HINTS", credential);
            startActivity(intent);
        } else {
            Log.e(TAG, "Hint Read: NOT OK");
            Toast.makeText(this, "Hint Read Failed", Toast.LENGTH_SHORT).show();
        }
    }

    ...

}

我已经为我的应用测试了它,credential.getId()包含您选择后可用于预填写表单字段的电子邮件地址.最后,我没有使用它,因为它只提供了使用Google帐户的选项,但是如果您只想要一个电子邮件地址,它会很好地工作!

I've tested it for my app and credential.getId() contains the email address you can use to prefill a form field after selection. In the end I'm not using it as it only gives the option to use Google Accounts but if you just want an email address it works very well!

这篇关于Android M电子邮件填写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆