Java 得到otp后,我想重定向到验证otp页面 package com.example.NewActivity; 导入android.content.Intent; 导入android.os.Bundle; 导入android.support.annotatio

Java 得到otp后,我想重定向到验证otp页面 package com.example.NewActivity; 导入android.content.Intent; 导入android.os.Bundle; 导入android.support.annotatio,java,android,Java,Android,得到otp后,我想重定向到验证otp页面 package com.example.NewActivity; 导入android.content.Intent; 导入android.os.Bundle; 导入android.support.annotation.NonNull; 导入android.support.v7.app.AppActivity; 导入android.util.Log; 导入android.view.view; 导入android.widget.Button; 导入andro

得到otp后,我想重定向到验证otp页面
package com.example.NewActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.annotation.NonNull;
导入android.support.v7.app.AppActivity;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.example.aayushchaubey.meetdax.R;
导入com.example.activity.HomeActivity;
导入com.google.android.gms.tasks.OnCompleteListener;
导入com.google.android.gms.tasks.Task;
导入com.google.firebase.FirebaseException;
导入com.google.firebase.FirebaseTooManyRequestsException;
导入com.google.firebase.auth.AuthResult;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
导入com.google.firebase.auth.FirebaseUser;
导入com.google.firebase.auth.PhoneAuthCredential;
导入com.google.firebase.auth.PhoneAuthProvider;
导入java.util.concurrent.TimeUnit;
/**
*由DIS015于2018年4月18日创建。
*/
公共类PhoneNumberRegistration扩展了AppCompative活动{
编辑文本电话号码;
按钮登录;
text查看不正确的电视,重新设置电视;
FirebaseAuth FirebaseAuth;
FirebaseAuth.AuthStateListener authListener;
PhoneAuthProvider.OnVerificationStateChangedCallbacks McCallbacks;
私有静态最终字符串标记=“FirebasePhoneNumAuth”;
PhoneAuthProvider.ForceResendingToken mResendToken;
字符串mvericationid;
字符串id,令牌;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u phone\u登录);
phoneNo_Edt=(EditText)findViewById(R.id.phoneNoEdt);
不正确电视=(文本视图)findViewById(R.id.icorrectTv);
loginBtn=(按钮)findViewById(R.id.phoneNoLoginBtn);
reSendOtpTv=(TextView)findviewbyd(R.id.resendCode);
电话号码设置单行();
firebaseAuth=firebaseAuth.getInstance();
addOnclickListener();
mCallbacks=新的PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
@凌驾
已完成验证的公共无效(PhoneAuthCredential凭据){
//此回调将在两种情况下调用:
//1-即时验证。在某些情况下,电话号码可以即时验证
//已验证,无需发送或输入验证代码。
//2-自动检索。在某些设备上,Google Play服务可以自动
//检测传入的验证SMS并执行验证,无需
//用户操作。
Log.d(标签“onVerificationCompleted:”+凭证);
使用PhoneAuthCredential(凭证)登录;
}
@凌驾
public void onVerificationFailed(FirebaseException e){
//在发出无效验证请求时调用此回调,
//例如,如果电话号码格式无效。
Log.w(标签“onVerificationFailed”,e);
if(例如FirebaseAuthInvalidCredentialsException实例){
//无效请求
// ...
}else if(例如FirebaseTooManyRequestsException的实例){
//已超过项目的SMS配额
// ...
}
//显示消息并更新UI
// ...
}
@凌驾
public void oncodesnt(字符串验证ID,
PhoneAuthProvider.ForceResendingToken(令牌){
//短信验证码已发送至提供的电话号码,我们
//现在需要要求用户输入代码,然后构造凭证
//通过将代码与验证ID相结合。
Log.d(标签“oncodesnt:+验证ID”);
//保存验证ID和重新发送令牌,以便稍后使用
mvericationId=验证ID;
mResendToken=令牌;
// ...
}
};
}
public void addOnclickListener(){
loginBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
sendSms();
}
});
reSendOtpTv.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
字符串phonnumber=phoneNo_Edt.getText().toString();
重新发送验证代码(电话号码、mResendToken);
}
});
}
@凌驾
public void onStart(){
super.onStart();
//检查用户是否已登录(非空),并相应地更新用户界面。
FirebaseUser currentUser=firebaseAuth.getCurrentUser();
如果(currentUser!=null){
updateUI();
完成();
}
}
公共void updateUI(){
Toast.makeText(PhoneNumberRegistration.this,“您已登录”,Toast.LENGTH\u SHORT.show();
Intent Intent=新的Intent(getApplicationContext(),HomeActivity.class);
星触觉(意向);
}
公共无效发送SMS(){
字符串phoneNumber=phoneNo_Edt.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber,//要验证的电话号码
60,//超时持续时间
TimeUnit.SECONDS,//超时单位
此,//活动(用于回调绑定)
mCallbacks);//OnVerificationStateChangedCallbacks
}
private void resendVerificationCode(字符串phoneNumber,
package com.example.NewActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.aayushchaubey.meetdax.R;
import com.example.activity.HomeActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.FirebaseTooManyRequestsException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;

/**
 * Created by DIS015 on 4/18/2018.
 */

public class PhoneNumberRegistration extends AppCompatActivity{

    EditText phoneNo_Edt;
    Button loginBtn;
    TextView incorrectTv,reSendOtpTv;
    FirebaseAuth firebaseAuth;
    FirebaseAuth.AuthStateListener authListener;
    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    private static final String TAG = "FirebasePhoneNumAuth";
    PhoneAuthProvider.ForceResendingToken mResendToken;
    String mVerificationId;
    String id,token;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phoneno_login);

        phoneNo_Edt=(EditText)findViewById(R.id.phoneNoEdt);
        incorrectTv = (TextView) findViewById(R.id.icorrectTv);
        loginBtn=(Button)findViewById(R.id.phoneNoLoginBtn);
        reSendOtpTv=(TextView)findViewById(R.id.resendCode);
        phoneNo_Edt.setSingleLine();
        firebaseAuth=FirebaseAuth.getInstance();

        addOnclickListener();

        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

            @Override
            public void onVerificationCompleted(PhoneAuthCredential credential) {
                // This callback will be invoked in two situations:
                // 1 - Instant verification. In some cases the phone number can be instantly
                //     verified without needing to send or enter a verification code.
                // 2 - Auto-retrieval. On some devices Google Play services can automatically
                //     detect the incoming verification SMS and perform verification without
                //     user action.
                Log.d(TAG, "onVerificationCompleted:" + credential);

                signInWithPhoneAuthCredential(credential);
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                // This callback is invoked in an invalid request for verification is made,
                // for instance if the the phone number format is not valid.
                Log.w(TAG, "onVerificationFailed", e);

                if (e instanceof FirebaseAuthInvalidCredentialsException) {
                    // Invalid request
                    // ...
                } else if (e instanceof FirebaseTooManyRequestsException) {
                    // The SMS quota for the project has been exceeded
                    // ...
                }

                // Show a message and update the UI
                // ...
            }

            @Override
            public void onCodeSent(String verificationId,
                                   PhoneAuthProvider.ForceResendingToken token) {
                // The SMS verification code has been sent to the provided phone number, we
                // now need to ask the user to enter the code and then construct a credential
                // by combining the code with a verification ID.
                Log.d(TAG, "onCodeSent:" + verificationId);

                // Save verification ID and resending token so we can use them later
                mVerificationId = verificationId;
                mResendToken = token;

                // ...
            }
        };


    }

    public void addOnclickListener(){
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendSms();
                }
        });
        reSendOtpTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phonrNumber=phoneNo_Edt.getText().toString();
                resendVerificationCode(phonrNumber,mResendToken);
            }
        });

        }

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = firebaseAuth.getCurrentUser();
        if (currentUser != null) {
            updateUI();
            finish();
        }
    }
    public void updateUI() {
        Toast.makeText(PhoneNumberRegistration.this, "you were logged in", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        startActivity(intent);
    }

    public void sendSms(){
        String phoneNumber=phoneNo_Edt.getText().toString();
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                this,               // Activity (for callback binding)
                mCallbacks);        // OnVerificationStateChangedCallbacks

    }

    private void resendVerificationCode(String phoneNumber,
                                        PhoneAuthProvider.ForceResendingToken token) {
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                this,               // Activity (for callback binding)
                mCallbacks,         // OnVerificationStateChangedCallbacks
                token);             // ForceResendingToken from callbacks
    }


    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        firebaseAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = task.getResult().getUser();

                            if(mVerificationId!=null){
                                Intent intent = new Intent(getApplicationContext(), VerifyOtp.class);
                                intent.putExtra("verificationcode",mVerificationId);
                                startActivity(intent);
                            }else{
                                updateUI();
                            }

                            // ...
                        } else {
                            // Sign in failed, display a message and update the UI
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                // The verification code entered was invalid
                            }
                        }
                    }
                });
    }


}