Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用android studio为firebase中的所有新用户设置默认值_Android_Firebase_Firebase Realtime Database_Firebase Authentication_Google Signin - Fatal编程技术网

如何使用android studio为firebase中的所有新用户设置默认值

如何使用android studio为firebase中的所有新用户设置默认值,android,firebase,firebase-realtime-database,firebase-authentication,google-signin,Android,Firebase,Firebase Realtime Database,Firebase Authentication,Google Signin,如何为所有首次使用google登录的新用户设置默认整数(0)值,但对于老用户(之前注册的用户),该值在清除应用程序的应用程序数据时不应更改 例如,我是第一次注册的新用户,我的分数为30分,在应用程序中,我玩一些任务以赚取更多,现在我得到60分,但当我再次删除应用程序数据时,我在登录时得到30分 请看代码 private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { Log.d(TAG, "firebaseAuthWithG

如何为所有首次使用google登录的新用户设置默认整数(0)值,但对于老用户(之前注册的用户),该值在清除应用程序的应用程序数据时不应更改

例如,我是第一次注册的新用户,我的分数为30分,在应用程序中,我玩一些任务以赚取更多,现在我得到60分,但当我再次删除应用程序数据时,我在登录时得到30分

请看代码

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    //getting the auth credential
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    //Now using firebase we are signing in the user here
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    progressDialog.dismiss();
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
                        DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());
                        if (user.getUid() != null){
                            UserId.getDatabase();
                            Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
                            Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
                            main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(main2);
                        }else {
                            UserId.child("Score").setValue(30);
                            Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
                            Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
                            main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(main2);
                        }


                    } else {
                        // If the sign in fails displays a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();

                    }


                }
            });
}

         UserId.child("Score").setValue(30);

         Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
         Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
                            main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(main2);
在我的firebase数据库中创建了带有分数的新uid,但问题是当我清除应用程序数据并再次登录时,分数变为30,但如果我不替换代码,则不会在数据库中创建新用户,但会使用分数null对其进行身份验证

那么,请告诉我,我如何才能做到这一点

我用他们的uid来追踪
谢谢,如果您有任何帮助,我们将不胜感激。

现在,我通过创建两个函数来检查用户是否是新用户,然后通过valueaddedeventlistener使用uid同时添加。

private void newUserScore(){
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
    final DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());

    UserId.child("Score").setValue(30);
    Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
    Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
    main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(main2);

}
这是登录的代码

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    //getting the auth credential
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    //Now using firebase we are signing in the user here
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    progressDialog.dismiss();
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithCredential:success");
                        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
                        FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
                        final DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());
                        final String uid = currentFirebaseUser.getUid();
                        databaseReference.child("Users").addListenerForSingleValueEvent(
                                new ValueEventListener() {

                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {

                                        //String uid1 = dataSnapshot.hasChild(uid);

                                        if (dataSnapshot.hasChild(uid)) {
                                            //Old User
                                            userAlreadyExistsScore();

                                        } else {
                                            // User Not Yet Exists
                                            newUserScore();
                                        }
                                    }
                                    @Override
                                    public void onCancelled (DatabaseError databaseError){

                                    }
                                }

                        );

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();

                    }

                    // ...
                }
            });
}
这里有用户代码已经存在

private void userAlreadyExistsScore(){

    FirebaseUser user = mAuth.getCurrentUser();
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
    final DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());


    if (user != null){

        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Integer Score = dataSnapshot.getValue(Integer.class);
                UserId.child("Score").setValue(Score);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        UserId.child("Score").addListenerForSingleValueEvent(valueEventListener);
        Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
        Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
        main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(main2);

    }
}

你的代码中有没有任何地方可以撤销用户访问权限?没有,我没有写撤销用户访问权限请查看更新的问题,谢谢
private void userAlreadyExistsScore(){

    FirebaseUser user = mAuth.getCurrentUser();
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
    final DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());


    if (user != null){

        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Integer Score = dataSnapshot.getValue(Integer.class);
                UserId.child("Score").setValue(Score);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        UserId.child("Score").addListenerForSingleValueEvent(valueEventListener);
        Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
        Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
        main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(main2);

    }
}