OTP 没有从 firebase 代码中获取是正确的

我想从 Firebase 获取 OTP 到我的手机以进行验证,我已经按照文档所述完成了所有操作,但最后我没有得到 OTP 请帮助我,我知道我的代码有问题,任何人都可以纠正我吗?我的应用程序已连接并使用 firebase 这是我的活动 java 代码:


public class LoginActivity extends AppCompatActivity {


String phone_number;

FirebaseAuth auth;

PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);


    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks(){

        @Override

        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {


        }


        @Override

        public void onVerificationFailed(FirebaseException e) {


        }

    };


    CardView card_view = findViewById(R.id.cardView);

    card_view.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            EditText phonetextview = findViewById(R.id.phonetextview);

            phone_number = PhoneNumberUtils.formatNumber(phonetextview.getText().toString());


            send();


            Toast toast = Toast.makeText(getApplicationContext(), phone_number,

                    Toast.LENGTH_SHORT);

            toast.show();

        }

    });

}


private void send() {


    PhoneAuthProvider.getInstance().verifyPhoneNumber(

            phone_number,        // Phone number to verify

            60,                 // Timeout duration

            TimeUnit.SECONDS,   // Unit of timeout

            this,               // Activity (for callback binding)

            mCallbacks);             // ForceResendingToken from callbacks

    }

}


九州编程
浏览 103回答 2
2回答

拉莫斯之舞

您需要intialize firebase在使用它之前。为此创建一个Application类 .ie :public class MyApplication extends Application {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate() {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate();&nbsp; &nbsp; &nbsp; &nbsp; FirebaseApp.initializeApp(this);//here we are intializing firebase&nbsp; &nbsp; }}也不要忘记将此添加到您的AndroidManifest.xml:<application&nbsp; &nbsp; &nbsp; &nbsp; android:name="yourPackage.MyApplication"&nbsp; &nbsp; &nbsp; &nbsp; android:allowBackup="true"&nbsp; &nbsp; &nbsp; &nbsp; android:icon="@mipmap/route"&nbsp; &nbsp; &nbsp; &nbsp; android:theme="@style/AppTheme">&nbsp;<!--your activity classes-->&nbsp; <activity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:name=".YourActivity"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >&nbsp;</application>

繁花如伊

确保您不在模拟器上,然后确保您已将有效的 SHA-1 密钥添加到 Firebase 控制台。试试这个来检查你得到了什么错误&nbsp;&nbsp;&nbsp;&nbsp;@Override&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;onVerificationFailed(FirebaseException&nbsp;e)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java