我需要在我的应用程序中检查用户帐户,并且我需要在应用程序中多次检查它,我使用了这个答案并且工作正常,但问题是每次我想获取帐户时,选择器对话框都会出现“选择一个帐户”它一点也不好,我很感激任何阻止它的解决方案!
问问题
259 次
2 回答
1
使用共享首选项存储选中的值,然后检查它是否存在,如果不存在则显示。!
在 Preference 中设置值:
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "user1");
editor.putInt("idName", 1);
editor.commit();
从首选项中检索数据:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
//Call this function in you OnCreate Method.!
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
Log.e(TAG, "display name: " + acct.getDisplayName());
personName = acct.getDisplayName();
email = acct.getEmail();
if(acct.getPhotoUrl()!= null) {
personPhotoUrl = acct.getPhotoUrl();
}
else
{
personPhotoUrl=Uri.parse("android.resource://com.compscitutorials.basigarcia.navigationdrawervideotutorial/drawable/add_profile_image");;
Log.d(TAG, "handleSignInResult: Photo Url is empty");
}
Log.e(TAG, "Name: " + personName + ", email: " + email
+ ", Image: " + personPhotoUrl.getPath().toString());
// txtName.setText(personName);
// txtEmail.setText(email);
// Glide.with(getApplicationContext()).load(personPhotoUrl)
// .thumbnail(0.5f)
// .crossFade()
// .diskCacheStrategy(DiskCacheStrategy.ALL)
// .into(imgProfilePic);
updateUI(true);
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
现在将您当前的用户与以前的用户进行比较。!
private void updateUI(boolean isSignedIn) {
if (isSignedIn) {
if(!currentName.equals(name))
{
//Next, launch the account chooser intent:
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE},
false, null, null, null, null);
try {
startActivityForResult(intent, REQUEST_CODE_EMAIL);
} catch (ActivityNotFoundException e) {
// This device may not have Google Play Services installed.
// TODO: do something else
}
}
} else {
// Display Toast SignUp Failed
Toast.makeText(this, "SignUp Error", Toast.LENGTH_SHORT).show();
}
}
//最后重写onActivityResult获取账户类型和账户名:
@ @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
于 2017-05-25T06:30:31.793 回答
0
您正在使用帐户选择器,因此您不应期望该对话框不会出现
于 2017-06-21T11:48:19.320 回答