2017-08-03 46 views
0

我有兩個活動的第一個活動的輸入數量以檢查AUTH從火力發OTP代碼,但我需要把代碼中的第二個活動與Textwatcher火力地堡電話認證介紹如何OTP密碼和訪問發送到另一個活動

在這裏我第一次活動代碼現在可以把數量和去次活動並獲得OTP代碼,並傳遞給第二活動

@Override 
     public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { 
      Toast.makeText(OTP1.this,"verifucation done"+ phoneAuthCredential,Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onVerificationFailed(FirebaseException e) { 
      Toast.makeText(OTP1.this,"verifucation fail",Toast.LENGTH_LONG).show(); 
      if (e instanceof FirebaseAuthInvalidCredentialsException) { 
       // Invalid request 
       // [START_EXCLUDE] 
       Toast.makeText(OTP1.this,"invalid mob no",Toast.LENGTH_LONG).show(); 
       // [END_EXCLUDE] 
      } else if (e instanceof FirebaseTooManyRequestsException) { 
       // The SMS quota for the project has been exceeded 
       // [START_EXCLUDE] 
       Toast.makeText(OTP1.this,"quta over" ,Toast.LENGTH_LONG).show(); 
       // [END_EXCLUDE] 
      } 
     } 
     @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); 
      Toast.makeText(OTP1.this,"Verification code sent to mobile",Toast.LENGTH_LONG).show(); 
      // Save verification ID and resending token so we can use them later 
      mVerificationId = verificationId; 

      mResendToken = token; 
      startActivity(new Intent(OTP1.this,OTP2.class)); 
      finish(); 
     } 
    }; 

    btn_next = (ImageButton)findViewById(R.id.nextButoon); 

    btn_next.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      PhoneAuthProvider.getInstance().verifyPhoneNumber(
        "+66"+ put_num.getText().toString(),  // Phone number to verify 
        60,     // Timeout duration 
        TimeUnit.SECONDS, // Unit of timeout 
        OTP1.this,    // Activity (for callback binding) 
        mCallbacks);  // OnVerificationStateChangedCallbacks 

     } 
    }); 

} 

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) { 
    mAuth.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"); 
         Toast.makeText(OTP1.this,"Verification done",Toast.LENGTH_LONG).show(); 
         FirebaseUser user = task.getResult().getUser(); 
         // ... 
        } 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 
          Toast.makeText(OTP1.this,"Verification failed code invalid",Toast.LENGTH_LONG).show(); 
         } 
        } 
       } 
      }); 
} 

而且我試圖使用Textwatcher一個簡單的代碼,並得到這個錯誤這是我的第二個活動

08-03 14:50:34.383 7790-7790/com.example.androiddev.army31 E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.androiddev.army31, PID: 7790 
                      java.lang.IllegalArgumentException: Given String is empty or null 
                       at com.google.android.gms.common.internal.zzbo.zzcF(Unknown Source) 
                       at com.google.firebase.auth.PhoneAuthCredential.<init>(Unknown Source) 
                       at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source) 
                       at com.example.androiddev.army31.LoginScreen.OTP2$1.onTextChanged(OTP2.java:57) 
                       at android.widget.TextView.sendOnTextChanged(TextView.java:8504) 
                       at android.widget.TextView.handleTextChanged(TextView.java:8566) 
                       at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10770) 
                       at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1212) 
                       at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:582) 
                       at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:509) 
                       at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:508) 
                       at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121) 
                       at android.widget.TextView.doKeyDown(TextView.java:6528) 
                       at android.widget.TextView.onKeyDown(TextView.java:6318) 
                       at android.view.KeyEvent.dispatch(KeyEvent.java:2740) 
                       at android.view.View.dispatchKeyEvent(View.java:9948) 

在這裏,我的第二個活動

pinView.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      if (pinView.getText().toString().trim().length() == 6){ 
       PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, pinView.getText().toString()); 
       signInWithPhoneAuthCredential(credential); 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 





} 

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) { 
    mAuth.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"); 
         Toast.makeText(OTP2.this,"Verification done",Toast.LENGTH_LONG).show(); 
         FirebaseUser user = task.getResult().getUser(); 
         // ... 
        } 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 
          Toast.makeText(OTP2.this,"Verification failed code invalid",Toast.LENGTH_LONG).show(); 
         } 
        } 
       } 
      }); 
} 

回答

1

這個問題對我來說,在我的google_services.json的API_KEY值是空的。這是因爲firebase爲我生成的默認json文件根本沒有api_key值,所以我只是手動添加一個文件以使其工作。在這裏添加一個隨機字符串值將使codelab工作,但確保停止當前運行並重建項目,否則Android Studio自動運行功能將運行而不更新正在使用的json文件。

您可以從中獲取您的api密鑰。 /console.firebase.google.com/project - >您的項目 - >設置 - >一般。

並將其添加到您的google_services.json中current_key

相關問題