电话重启后获取OTP

时间:2019-01-18 21:21:44

标签: android firebase android-studio firebase-authentication

我正在为我的项目使用以下Java代码,并从Firebase接收OTP编号。如果我是第一次输入电话号码,代码可以正常工作。第二次起,我没有收到OTP号码。为了获得OTP号码,我需要每次重新启动手机。如果我关闭并重新打开该应用程序,则它也无济于事。我的日志中没有任何错误。

每次我重新启动手机时,都会从Firebase获取OTP。

VeriyPhoneActivity

public class VerifyPhoneActivity extends AppCompatActivity {
    private String verificationId;
    private FirebaseAuth mAuth;
    private ProgressBar progressBar;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verify_phone);
    mAuth = FirebaseAuth.getInstance();
    progressBar = findViewById(R.id.progressbar);
    editText = findViewById(R.id.editTextCode);
    String phonenumber = getIntent().getStringExtra("phonenumber");
    sendVerificationCode(phonenumber);
    findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    String code = editText.getText().toString().trim();
    if (code.isEmpty() || code.length() < 6) {
    editText.setError("Enter code...");
    editText.requestFocus();
    return;
    }
    verifyCode(code);
    }
    });
    }
    private void verifyCode(String code) {
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
    signInWithCredential(credential);
    }
    private void signInWithCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
    if (task.isSuccessful()) {
    Intent intent = new Intent(VerifyPhoneActivity.this, Logout.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
    } else {
    Toast.makeText(VerifyPhoneActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
    }
    }
    });
    }

    private void sendVerificationCode(String number) {
    progressBar.setVisibility(View.VISIBLE);
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
    number,
    60,
    TimeUnit.SECONDS,
    TaskExecutors.MAIN_THREAD,
    mCallBack
    );
    }
    private PhoneAuthProvider.OnVerificationStateChangedCallbacks
    mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    @Override
    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
    super.onCodeSent(s, forceResendingToken);
    verificationId = s;
    }
    @Override
    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
    String code = phoneAuthCredential.getSmsCode();
    if (code != null) {
    editText.setText(code);
    verifyCode(code);
    }
    }
    @Override
    public void onVerificationFailed(FirebaseException e) {
    Toast.makeText(VerifyPhoneActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    };
    }

主要活动

public class MainActivity extends AppCompatActivity {
    private Spinner spinner;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirebaseApp.initializeApp(this);
        spinner = findViewById(R.id.spinnerCountries);
        spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, CountryData.countryNames));
        editText = findViewById(R.id.editTextPhone);
        findViewById(R.id.buttonContinue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = CountryData.countryAreaCodes[spinner.getSelectedItemPosition()];
                String number = editText.getText().toString().trim();
                if (number.isEmpty() || number.length() < 10) {
                    editText.setError("Valid number is required");
                    editText.requestFocus();
                    return;
                }
                String phoneNumber = "+" + code + number;
                Intent intent = new Intent(MainActivity.this, VerifyPhoneActivity.class);
                intent.putExtra("phonenumber", phoneNumber);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        // FirebaseApp.initializeApp(this);
        if (FirebaseAuth.getInstance().getCurrentUser() != null) {
            Intent intent = new Intent(this, Logout.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    }
}

注销

class Logout : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_logout)
        FirebaseAuth.getInstance().signOut()
        val phonenumber = intent.getStringExtra("phonenumber")
             Timer().schedule(5000)
    }
}

0 个答案:

没有答案