Home > Net >  How do I make the terms and conditions appear only when I log in to Google for the first time?
How do I make the terms and conditions appear only when I log in to Google for the first time?

Time:11-23

I want to add terms and conditions when users use my app. But I have to show terms and conditions only one time.

I'm using firebase google login. But I have a problem when I show terms and conditions and how code remembers users accept terms and conditions and shows only one time.

Here's a google login code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    //새로운 코드다 마

    signInButton = findViewById(R.id.signInButton);

    GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    googleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this,this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
            .build();

    auth = FirebaseAuth.getInstance(); //파이어 베이스 인증 객체 초기화
    signInButton.setOnClickListener(new View.OnClickListener() { // 구글 로그인 버튼을 클릭했을때 이곳을 수행
        @Override
        public void onClick(View view) {
            Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
            startActivityForResult(intent,REQ_SIGN_GOOGLE);




        }
    });


}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { // 구글 로그인 인증을 요청했을때 결과 값을 되돌려 받는 곳
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQ_SIGN_GOOGLE) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if(result.isSuccess() == true) { // true 생략 가능, 인증 결과가 성공적이면
            GoogleSignInAccount account = result.getSignInAccount(); //account 라는 데이터는 구글 로그인 정보를 담고 있습니다. - 닉네임,프로필사진uri,이메일 주소등
            resultLogin(account); // 로그인 결과 값 출력 수행하라는 메서드
        }
    }
}

private void resultLogin(GoogleSignInAccount account) {

    AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
    auth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful()) { //로그인이 성공했으면

                Intent intent = new Intent(getApplicationContext(),Home.class);
                startActivity(intent);

            }
            else{
                Toast.makeText(LoginActivity.this,"로그인 실패",Toast.LENGTH_SHORT).show();

            }
        }
    });
}

@Override
public void onBackPressed() {
    long curTime=System.currentTimeMillis();
    long gapTime=curTime-backBtnTime;

    if(0<=gapTime && 2000>= gapTime){
        moveTaskToBack(true);
        finishAndRemoveTask();
        android.os.Process.killProcess(android.os.Process.myPid());

    }
    else {
        backBtnTime = curTime;
        Toast.makeText(this,"뒤로 버튼을 한 번 더 누르면 종료됩니다.",Toast.LENGTH_SHORT).show();
    }
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

Here is a code I want to show (terms and conditions)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_terms);
    CheckBox checkBox=findViewById(R.id.checkbox);
    CheckBox checkBox2=findViewById(R.id.checkbox2);
    CheckBox checkBox3=findViewById(R.id.checkbox3);
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(checkBox.isChecked()){
                checkBox2.setChecked(true);
                checkBox3.setChecked(true);

            }else {
                checkBox2.setChecked(false);
                checkBox3.setChecked(false);

            }
        }
    });

    checkBox2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           
            if(checkBox.isChecked()){
                checkBox.setChecked(false);
                
            }else if(checkBox2.isChecked()&&checkBox3.isChecked()){
                checkBox.setChecked(true);
            }
        }
    });

    checkBox3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(checkBox.isChecked()){
                checkBox.setChecked(false);
            }else if(checkBox2.isChecked()&&checkBox3.isChecked()){
                checkBox.setChecked(true);
            }
        }
    });


    Button btn_agr = findViewById(R.id.btn_agr1);
    btn_agr.setText(R.string.app_name);
    btn_agr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(TermsActivity.this);
           
            builder.setTitle("서비스 이용약관 ");
            
            builder.setMessage(R.string.app_name); 
           
            builder.setNegativeButton("닫기",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            System.out.println(TAG   "이용약관 닫기");
                        }
                    });
           
            builder.show();
        }
    });


    Button btn_agr2 = findViewById(R.id.btn_agr2);
    btn_agr2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(TermsActivity.this);
           
            builder.setTitle("위치 정보 이용 약관 ");
            
            builder.setMessage(R.string.app_name); 
            builder.setNegativeButton("닫기",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            System.out.println(TAG   "이용약관 닫기");
                        }
                    });
    
            builder.show();
        }
    });

    Button btn_complete = findViewById(R.id.button_complete);
    btn_complete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });


}

I tried my self - transmit variables when the user accept terms and conditions but when the user reopens the app user has to open it again Maybe there is a way to transmit certain variables to firebase and confirm it when the user clicks the login button??

CodePudding user response:

There is a way to transmit certain variables to firebase and confirm it when the user clicks the login button?

The simplest solution would be to call AuthResult#getAdditionalUserInfo() method on the Task object, because it returns an object of type AdditionalUserInfo. In this class, you can find a very useful method called isNewUser(), which can help you check if the user signs in for the first time. In code should look like this:

AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
auth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if(task.isSuccessful()) { //로그인이 성공했으면

            boolean isNewUser = task.getResult().getAdditionalUserInfo().isNewUser();
            if (isNewUser) {
                //Display Terms & Conditions
            }

            Intent intent = new Intent(getApplicationContext(),Home.class);
            startActivity(intent);

        }
        else{
            Toast.makeText(LoginActivity.this,"로그인 실패",Toast.LENGTH_SHORT).show();

        }
    }
});
  • Related