我正在尝试在Android中打开SignIn Configuration
,但无法打开。
每当我尝试发生该错误时,我都不知道是什么原因。
当我使用Log
时,它会打印"success"
。
我无法解决该问题,请帮助。 看我的代码:
登录课程
public class LoginActivity extends AppCompatActivity {
private EditText loginEmail, loginPass;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
private Button loginBtn;
private SignInButton sButton;
static ProgressDialog mProgress;
private GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN= 1;
private FirebaseAuth.AuthStateListener mAuthListener;
private static final String TAG ="LOGINACTIVITY :";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mProgress = new ProgressDialog(this);
sButton=(SignInButton)findViewById(R.id.google_button);
loginBtn =(Button)findViewById(R.id.loginBtn);
loginEmail =(EditText)findViewById(R.id.login_email);
loginPass =(EditText)findViewById(R.id.login_password);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabase.keepSynced(true);
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (mAuth.getCurrentUser()==null){
Intent loginIntent = new Intent(LoginActivity.this, RegisterActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
}
}
};
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(LoginActivity.this, "PROCESSING....", Toast.LENGTH_LONG).show();
String email = loginEmail.getText().toString().trim();
String password = loginPass.getText().toString().trim();
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
mProgress.setMessage("Check login....");
mProgress.show();
mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
mProgress.dismiss();
checkUserExistence();
}else{
mProgress.dismiss();
Toast.makeText(LoginActivity.this, "Couldn't login, User not found", Toast.LENGTH_SHORT).show();
}
}
});
}else {
Toast.makeText(LoginActivity.this, "Complete all fields", Toast.LENGTH_SHORT).show();
}
}
});
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()).enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(LoginActivity.this, "get error", Toast.LENGTH_LONG).show();
}
}).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
sButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}
@Override
protected void onStart() {
super.onStart();
// checkUserExistence();
// mAuth.addAuthStateListener(mAuthListener);
}
private void signIn(){
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@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){
// mProgress.setMessage("Starting sighn in....");
// mProgress.show();
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
Log.w(TAG, "Google sign in Successful");
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
}catch (ApiException e) {
//Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// mProgress.dismiss();
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:success" + task.isSuccessful());
if (!task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Toast.makeText(LoginActivity.this, "Aut field", Toast.LENGTH_LONG).show();
Log.d(TAG, "signInWithCredential:success");
}
}
});
}
public void checkUserExistence(){
final String user_id = mAuth.getCurrentUser().getUid();
mDatabase.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if (dataSnapshot.hasChild(user_id)){
Intent intent = new Intent(LoginActivity.this, SutepActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
//startActivity(new Intent(LoginActivity.this, MainActivity.class));
}else {
//Toast.makeText(LoginActivity.this, "User not registered!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, SutepActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
成绩
buildscript {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
maven { url "https://jitpack.io" }
mavenCentral()
mavenLocal()
}
dependencies {
// classpath 'com.android.tools.build:gradle:2.3.3'
// classpath 'com.google.gms:google-services:4.2.0'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:4.2.0'
// classpath 'com.google.gms:google-services:3.0.0'
//classpath 'com.google.gms:google-services:4.0.1'
// classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
maven { url "https://jitpack.io" }
mavenCentral()
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
dexOptions {
//javaMaxHeapSize "2048M"
preDexLibraries = false
javaMaxHeapSize "2g"
// javaMaxHeapSize "2g"
}
configurations.all {
resolutionStrategy.force 'com.android.support:multidex:1.0.3'
}
defaultConfig {
applicationId "com.ahmed.ekene.blogzone"
// Enabling multidex support.
multiDexEnabled true
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
//compile 'com.android.support:multidex:1.0.3'
compile 'com.android.support:appcompat-v7:28+'
compile 'com.android.support:design:28.0.0'
compile 'com.android.support:cardview-v7:28.0.0-alpha1'
compile 'com.android.support:recyclerview-v7:28.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.theartofdev.edmodo:android-image-cropper:2.5.+'
compile 'com.firebaseui:firebase-ui-database:0.4.4'
compile 'com.google.firebase:firebase-core:16.0.8'
// compile 'com.google.firebase:firebase-database:10.0.1'
/// instance
compile 'com.google.firebase:firebase-database:16.1.0'
compile 'com.google.firebase:firebase-storage:16.1.0'
//compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-auth:16.2.1'
compile 'com.google.android.gms:play-services-auth:16.0.1'
//implementation 'com.squareup.picasso:picasso:2.71828'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'