Android M email completition
as you know the permission system on Android M has been updated.
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));
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 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
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-passwords/android/retrieve-hints
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.
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();
}
}
...
}
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!
链接地址: http://www.djcxy.com/p/70164.html上一篇: 用名称中的括号([&])上载文件
下一篇: Android M电子邮件完成