IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

La verification ne fait que rate


Sujet :

Android

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Avril 2016
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Congo-Brazzaville

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Services à domicile

    Informations forums :
    Inscription : Avril 2016
    Messages : 9
    Points : 7
    Points
    7
    Par défaut La verification ne fait que rate
    bonjour
    Je suis assez nouveau dans le developement android et j ai un soucis avec l authentification en utilisant firebase. L authentification ne reussi pas.
    J ai vraiment besoin d aide je suis reelement perdu.
    CODE JAVA
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    package com.example.programmer.bmarketversion3;
     
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.support.annotation.NonNull;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.text.TextUtils;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.TextView;
     
    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;
     
    public class Login2 extends AppCompatActivity  implements
            View.OnClickListener {
     
        private static final String TAG = "PhoneAuthActivity";
     
        private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";
     
        private static final int STATE_INITIALIZED = 1;
        private static final int STATE_CODE_SENT = 2;
        private static final int STATE_VERIFY_FAILED = 3;
        private static final int STATE_VERIFY_SUCCESS = 4;
        private static final int STATE_SIGNIN_FAILED = 5;
        private static final int STATE_SIGNIN_SUCCESS = 6;
     
        // [START declare_auth]
        private FirebaseAuth mAuth;
        // [END declare_auth]
     
        private boolean mVerificationInProgress = false;
        private String mVerificationId;
        private PhoneAuthProvider.ForceResendingToken mResendToken;
        private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
     
        private ViewGroup mPhoneNumberViews;
        private ViewGroup mSignedInViews;
     
        private TextView mStatusText;
        private TextView mDetailText;
     
        private EditText mPhoneNumberField;
        private EditText mVerificationField;
     
        private Button mStartButton;
        private Button mVerifyButton;
        private Button mResendButton;
        private Button mSignOutButton;
     
        ProgressBar progressBar;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login2);
     
            if (savedInstanceState != null) {
                onRestoreInstanceState(savedInstanceState);
            }
     
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
     
            // Assign views
            mPhoneNumberViews = (ViewGroup) findViewById(R.id.phone_auth_fields);
            mSignedInViews = (ViewGroup) findViewById(R.id.signed_in_buttons);
     
            mStatusText = (TextView) findViewById(R.id.status);
            mDetailText = (TextView) findViewById(R.id.detail);
     
            mPhoneNumberField = (EditText) findViewById(R.id.field_phone_number);
            mVerificationField = (EditText) findViewById(R.id.field_verification_code);
     
            mStartButton = (Button) findViewById(R.id.button_start_verification);
            mVerifyButton = (Button) findViewById(R.id.button_verify_phone);
            mResendButton = (Button) findViewById(R.id.button_resend);
            mSignOutButton = (Button) findViewById(R.id.sign_out_button);
     
            // Assign click listeners
            mStartButton.setOnClickListener(this);
            mVerifyButton.setOnClickListener(this);
            mResendButton.setOnClickListener(this);
            mSignOutButton.setOnClickListener(this);
     
            // [START initialize_auth]
            mAuth = FirebaseAuth.getInstance();
            // [END initialize_auth]
     
            // Initialize phone auth callbacks
            // [START phone_auth_callbacks]
            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 verificaiton without
                    //     user action.
                    Log.d(TAG, "onVerificationCompleted:" + credential);
                    // [START_EXCLUDE silent]
                    mVerificationInProgress = false;
                    // [END_EXCLUDE]
     
                    // [START_EXCLUDE silent]
                    // Update the UI and attempt sign in with the phone credential
                    updateUI(STATE_VERIFY_SUCCESS, credential);
                    // [END_EXCLUDE]
                    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);
                    // [START_EXCLUDE silent]
                    mVerificationInProgress = false;
                    // [END_EXCLUDE]
     
                    if (e instanceof FirebaseAuthInvalidCredentialsException) {
                        // Invalid request
                        // [START_EXCLUDE]
                        mPhoneNumberField.setError("Invalid phone number.");
                        // [END_EXCLUDE]
                    } else if (e instanceof FirebaseTooManyRequestsException) {
                        // The SMS quota for the project has been exceeded
                        // [START_EXCLUDE]
                        Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
                                Snackbar.LENGTH_SHORT).show();
                        // [END_EXCLUDE]
                    }
     
                    // Show a message and update the UI
                    // [START_EXCLUDE]
                    updateUI(STATE_VERIFY_FAILED);
                    // [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);
     
                    // Save verification ID and resending token so we can use them later
                    mVerificationId = verificationId;
                    mResendToken = token;
     
                    // [START_EXCLUDE]
                    // Update UI
                    updateUI(STATE_CODE_SENT);
                    // [END_EXCLUDE]
                }
            };
            // [END phone_auth_callbacks]
        }
     
        // [START on_start_check_user]
        @Override
        public void onStart() {
            super.onStart();
            // Check if user is signed in (non-null) and update UI accordingly.
            FirebaseUser currentUser = mAuth.getCurrentUser();
            updateUI(currentUser);
     
            // [START_EXCLUDE]
            if (mVerificationInProgress && validatePhoneNumber()) {
                startPhoneNumberVerification(mPhoneNumberField.getText().toString());
            }
            // [END_EXCLUDE]
        }
        // [END on_start_check_user]
     
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putBoolean(KEY_VERIFY_IN_PROGRESS, mVerificationInProgress);
        }
     
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            mVerificationInProgress = savedInstanceState.getBoolean(KEY_VERIFY_IN_PROGRESS);
        }
     
     
        private void startPhoneNumberVerification(String phoneNumber) {
            // [START start_phone_auth]
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                    phoneNumber,        // Phone number to verify
                    60,                 // Timeout duration
                    TimeUnit.SECONDS,   // Unit of timeout
                    this,               // Activity (for callback binding)
                    mCallbacks);        // OnVerificationStateChangedCallbacks
            // [END start_phone_auth]
     
            mVerificationInProgress = true;
            mStatusText.setVisibility(View.INVISIBLE);
        }
     
        private void verifyPhoneNumberWithCode(String verificationId, String code) {
            // [START verify_with_code]
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
            // [END verify_with_code]
            signInWithPhoneAuthCredential(credential);
        }
     
        // [START resend_verification]
        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
        }
        // [END resend_verification]
     
        // [START sign_in_with_phone]
        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");
     
                                FirebaseUser user = task.getResult().getUser();
                                // [START_EXCLUDE]
                                updateUI(STATE_SIGNIN_SUCCESS, user);
                                // [END_EXCLUDE]
                            } 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
                                    // [START_EXCLUDE silent]
                                    mVerificationField.setError("Invalid code.");
                                    // [END_EXCLUDE]
                                }
                                // [START_EXCLUDE silent]
                                // Update UI
                                updateUI(STATE_SIGNIN_FAILED);
                                // [END_EXCLUDE]
                            }
                        }
                    });
        }
        // [END sign_in_with_phone]
     
        private void signOut() {
            mAuth.signOut();
            updateUI(STATE_INITIALIZED);
        }
     
        private void updateUI(int uiState) {
            updateUI(uiState, mAuth.getCurrentUser(), null);
        }
     
        private void updateUI(FirebaseUser user) {
            if (user != null) {
                updateUI(STATE_SIGNIN_SUCCESS, user);
            } else {
                updateUI(STATE_INITIALIZED);
            }
        }
     
        private void updateUI(int uiState, FirebaseUser user) {
            updateUI(uiState, user, null);
        }
     
        private void updateUI(int uiState, PhoneAuthCredential cred) {
            updateUI(uiState, null, cred);
        }
     
        private void updateUI(int uiState, FirebaseUser user, PhoneAuthCredential cred) {
            switch (uiState) {
                case STATE_INITIALIZED:
                    // Initialized state, show only the phone number field and start button
                    enableViews(mStartButton, mPhoneNumberField);
                    disableViews(mVerifyButton, mResendButton, mVerificationField);
                    mDetailText.setText(null);
                    break;
                case STATE_CODE_SENT:
                    // Code sent state, show the verification field, the
                    enableViews(mVerifyButton, mResendButton, mPhoneNumberField, mVerificationField);
                    disableViews(mStartButton);
                    mDetailText.setText(R.string.status_code_sent);
                    mDetailText.setTextColor(Color.parseColor("#43a047"));
                    break;
                case STATE_VERIFY_FAILED:
                    // Verification has failed, show all options
                    enableViews(mStartButton, mVerifyButton, mResendButton, mPhoneNumberField,
                            mVerificationField);
                    mDetailText.setText(R.string.status_verification_failed);
                    mDetailText.setTextColor(Color.parseColor("#dd2c00"));
                    progressBar.setVisibility(View.INVISIBLE);
                    break;
                case STATE_VERIFY_SUCCESS:
                    // Verification has succeeded, proceed to firebase sign in
                    disableViews(mStartButton, mVerifyButton, mResendButton, mPhoneNumberField,
                            mVerificationField);
                    mDetailText.setText("Verfication Sucessfull");
                    mDetailText.setTextColor(Color.parseColor("#43a047"));
                    progressBar.setVisibility(View.INVISIBLE);
     
                    // Set the verification text based on the credential
                    if (cred != null) {
                        if (cred.getSmsCode() != null) {
                            mVerificationField.setText(cred.getSmsCode());
                        } else {
                            mVerificationField.setText(R.string.instant_validation);
                            mVerificationField.setTextColor(Color.parseColor("#4bacb8"));
                        }
                    }
     
                    break;
                case STATE_SIGNIN_FAILED:
                    // No-op, handled by sign-in check
                    mDetailText.setText(R.string.status_sign_in_failed);
                    mDetailText.setTextColor(Color.parseColor("#dd2c00"));
                    progressBar.setVisibility(View.INVISIBLE);
                    break;
                case STATE_SIGNIN_SUCCESS:
                    // Np-op, handled by sign-in check
                    mStatusText.setText(R.string.signed_in);
                    break;
            }
     
            if (user == null) {
                // Signed out
                mPhoneNumberViews.setVisibility(View.VISIBLE);
                mSignedInViews.setVisibility(View.GONE);
     
                mStatusText.setText(R.string.signed_out);;
            } else {
                // Signed in
                mPhoneNumberViews.setVisibility(View.GONE);
                /*
                mSignedInViews.setVisibility(View.VISIBLE);
     
                enableViews(mPhoneNumberField, mVerificationField);
                mPhoneNumberField.setText(null);
                mVerificationField.setText(null);
     
                mStatusText.setText(R.string.signed_in);
                mDetailText.setText(getString(R.string.firebase_status_fmt, user.getUid()));
                */
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
     
            }
        }
     
        private boolean validatePhoneNumber() {
            String phoneNumber = mPhoneNumberField.getText().toString();
            if (TextUtils.isEmpty(phoneNumber)) {
                mPhoneNumberField.setError("Invalid phone number.");
                //mPhoneNumberField.setTextColor(Color.parseColor("#ff1744"));
                return false;
            }
     
            return true;
        }
     
        private void enableViews(View... views) {
            for (View v : views) {
                v.setEnabled(true);
            }
        }
     
        private void disableViews(View... views) {
            for (View v : views) {
                v.setEnabled(false);
            }
        }
     
     
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.button_start_verification:
                    if (!validatePhoneNumber()) {
                        return;
                    }
     
                    ///////hide keyboard start
                    InputMethodManager inputManager = (InputMethodManager)
                            getSystemService(Context.INPUT_METHOD_SERVICE);
     
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    /////////hide keyboard end
     
     
                    //mStatusText.setText("Authenticating....!");
                    progressBar.setVisibility(View.VISIBLE);
                    startPhoneNumberVerification(mPhoneNumberField.getText().toString());
     
                    break;
                case R.id.button_verify_phone:
                    String code = mVerificationField.getText().toString();
                    if (TextUtils.isEmpty(code)) {
                        mVerificationField.setError("Cannot be empty.");
                        return;
                    }
     
                    verifyPhoneNumberWithCode(mVerificationId, code);
                    break;
                case R.id.button_resend:
                    resendVerificationCode(mPhoneNumberField.getText().toString(), mResendToken);
                    break;
                case R.id.sign_out_button:
                    signOut();
                    break;
            }
        }
    }
    BUILD GRADLE
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    package com.example.programmer.bmarketversion3;
     
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.support.annotation.NonNull;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.text.TextUtils;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.TextView;
     
    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;
     
    public class Login2 extends AppCompatActivity  implements
            View.OnClickListener {
     
        private static final String TAG = "PhoneAuthActivity";
     
        private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";
     
        private static final int STATE_INITIALIZED = 1;
        private static final int STATE_CODE_SENT = 2;
        private static final int STATE_VERIFY_FAILED = 3;
        private static final int STATE_VERIFY_SUCCESS = 4;
        private static final int STATE_SIGNIN_FAILED = 5;
        private static final int STATE_SIGNIN_SUCCESS = 6;
     
        // [START declare_auth]
        private FirebaseAuth mAuth;
        // [END declare_auth]
     
        private boolean mVerificationInProgress = false;
        private String mVerificationId;
        private PhoneAuthProvider.ForceResendingToken mResendToken;
        private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
     
        private ViewGroup mPhoneNumberViews;
        private ViewGroup mSignedInViews;
     
        private TextView mStatusText;
        private TextView mDetailText;
     
        private EditText mPhoneNumberField;
        private EditText mVerificationField;
     
        private Button mStartButton;
        private Button mVerifyButton;
        private Button mResendButton;
        private Button mSignOutButton;
     
        ProgressBar progressBar;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login2);
     
            if (savedInstanceState != null) {
                onRestoreInstanceState(savedInstanceState);
            }
     
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
     
            // Assign views
            mPhoneNumberViews = (ViewGroup) findViewById(R.id.phone_auth_fields);
            mSignedInViews = (ViewGroup) findViewById(R.id.signed_in_buttons);
     
            mStatusText = (TextView) findViewById(R.id.status);
            mDetailText = (TextView) findViewById(R.id.detail);
     
            mPhoneNumberField = (EditText) findViewById(R.id.field_phone_number);
            mVerificationField = (EditText) findViewById(R.id.field_verification_code);
     
            mStartButton = (Button) findViewById(R.id.button_start_verification);
            mVerifyButton = (Button) findViewById(R.id.button_verify_phone);
            mResendButton = (Button) findViewById(R.id.button_resend);
            mSignOutButton = (Button) findViewById(R.id.sign_out_button);
     
            // Assign click listeners
            mStartButton.setOnClickListener(this);
            mVerifyButton.setOnClickListener(this);
            mResendButton.setOnClickListener(this);
            mSignOutButton.setOnClickListener(this);
     
            // [START initialize_auth]
            mAuth = FirebaseAuth.getInstance();
            // [END initialize_auth]
     
            // Initialize phone auth callbacks
            // [START phone_auth_callbacks]
            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 verificaiton without
                    //     user action.
                    Log.d(TAG, "onVerificationCompleted:" + credential);
                    // [START_EXCLUDE silent]
                    mVerificationInProgress = false;
                    // [END_EXCLUDE]
     
                    // [START_EXCLUDE silent]
                    // Update the UI and attempt sign in with the phone credential
                    updateUI(STATE_VERIFY_SUCCESS, credential);
                    // [END_EXCLUDE]
                    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);
                    // [START_EXCLUDE silent]
                    mVerificationInProgress = false;
                    // [END_EXCLUDE]
     
                    if (e instanceof FirebaseAuthInvalidCredentialsException) {
                        // Invalid request
                        // [START_EXCLUDE]
                        mPhoneNumberField.setError("Invalid phone number.");
                        // [END_EXCLUDE]
                    } else if (e instanceof FirebaseTooManyRequestsException) {
                        // The SMS quota for the project has been exceeded
                        // [START_EXCLUDE]
                        Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
                                Snackbar.LENGTH_SHORT).show();
                        // [END_EXCLUDE]
                    }
     
                    // Show a message and update the UI
                    // [START_EXCLUDE]
                    updateUI(STATE_VERIFY_FAILED);
                    // [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);
     
                    // Save verification ID and resending token so we can use them later
                    mVerificationId = verificationId;
                    mResendToken = token;
     
                    // [START_EXCLUDE]
                    // Update UI
                    updateUI(STATE_CODE_SENT);
                    // [END_EXCLUDE]
                }
            };
            // [END phone_auth_callbacks]
        }
     
        // [START on_start_check_user]
        @Override
        public void onStart() {
            super.onStart();
            // Check if user is signed in (non-null) and update UI accordingly.
            FirebaseUser currentUser = mAuth.getCurrentUser();
            updateUI(currentUser);
     
            // [START_EXCLUDE]
            if (mVerificationInProgress && validatePhoneNumber()) {
                startPhoneNumberVerification(mPhoneNumberField.getText().toString());
            }
            // [END_EXCLUDE]
        }
        // [END on_start_check_user]
     
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putBoolean(KEY_VERIFY_IN_PROGRESS, mVerificationInProgress);
        }
     
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            mVerificationInProgress = savedInstanceState.getBoolean(KEY_VERIFY_IN_PROGRESS);
        }
     
     
        private void startPhoneNumberVerification(String phoneNumber) {
            // [START start_phone_auth]
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                    phoneNumber,        // Phone number to verify
                    60,                 // Timeout duration
                    TimeUnit.SECONDS,   // Unit of timeout
                    this,               // Activity (for callback binding)
                    mCallbacks);        // OnVerificationStateChangedCallbacks
            // [END start_phone_auth]
     
            mVerificationInProgress = true;
            mStatusText.setVisibility(View.INVISIBLE);
        }
     
        private void verifyPhoneNumberWithCode(String verificationId, String code) {
            // [START verify_with_code]
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
            // [END verify_with_code]
            signInWithPhoneAuthCredential(credential);
        }
     
        // [START resend_verification]
        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
        }
        // [END resend_verification]
     
        // [START sign_in_with_phone]
        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");
     
                                FirebaseUser user = task.getResult().getUser();
                                // [START_EXCLUDE]
                                updateUI(STATE_SIGNIN_SUCCESS, user);
                                // [END_EXCLUDE]
                            } 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
                                    // [START_EXCLUDE silent]
                                    mVerificationField.setError("Invalid code.");
                                    // [END_EXCLUDE]
                                }
                                // [START_EXCLUDE silent]
                                // Update UI
                                updateUI(STATE_SIGNIN_FAILED);
                                // [END_EXCLUDE]
                            }
                        }
                    });
        }
        // [END sign_in_with_phone]
     
        private void signOut() {
            mAuth.signOut();
            updateUI(STATE_INITIALIZED);
        }
     
        private void updateUI(int uiState) {
            updateUI(uiState, mAuth.getCurrentUser(), null);
        }
     
        private void updateUI(FirebaseUser user) {
            if (user != null) {
                updateUI(STATE_SIGNIN_SUCCESS, user);
            } else {
                updateUI(STATE_INITIALIZED);
            }
        }
     
        private void updateUI(int uiState, FirebaseUser user) {
            updateUI(uiState, user, null);
        }
     
        private void updateUI(int uiState, PhoneAuthCredential cred) {
            updateUI(uiState, null, cred);
        }
     
        private void updateUI(int uiState, FirebaseUser user, PhoneAuthCredential cred) {
            switch (uiState) {
                case STATE_INITIALIZED:
                    // Initialized state, show only the phone number field and start button
                    enableViews(mStartButton, mPhoneNumberField);
                    disableViews(mVerifyButton, mResendButton, mVerificationField);
                    mDetailText.setText(null);
                    break;
                case STATE_CODE_SENT:
                    // Code sent state, show the verification field, the
                    enableViews(mVerifyButton, mResendButton, mPhoneNumberField, mVerificationField);
                    disableViews(mStartButton);
                    mDetailText.setText(R.string.status_code_sent);
                    mDetailText.setTextColor(Color.parseColor("#43a047"));
                    break;
                case STATE_VERIFY_FAILED:
                    // Verification has failed, show all options
                    enableViews(mStartButton, mVerifyButton, mResendButton, mPhoneNumberField,
                            mVerificationField);
                    mDetailText.setText(R.string.status_verification_failed);
                    mDetailText.setTextColor(Color.parseColor("#dd2c00"));
                    progressBar.setVisibility(View.INVISIBLE);
                    break;
                case STATE_VERIFY_SUCCESS:
                    // Verification has succeeded, proceed to firebase sign in
                    disableViews(mStartButton, mVerifyButton, mResendButton, mPhoneNumberField,
                            mVerificationField);
                    mDetailText.setText("Verfication Sucessfull");
                    mDetailText.setTextColor(Color.parseColor("#43a047"));
                    progressBar.setVisibility(View.INVISIBLE);
     
                    // Set the verification text based on the credential
                    if (cred != null) {
                        if (cred.getSmsCode() != null) {
                            mVerificationField.setText(cred.getSmsCode());
                        } else {
                            mVerificationField.setText(R.string.instant_validation);
                            mVerificationField.setTextColor(Color.parseColor("#4bacb8"));
                        }
                    }
     
                    break;
                case STATE_SIGNIN_FAILED:
                    // No-op, handled by sign-in check
                    mDetailText.setText(R.string.status_sign_in_failed);
                    mDetailText.setTextColor(Color.parseColor("#dd2c00"));
                    progressBar.setVisibility(View.INVISIBLE);
                    break;
                case STATE_SIGNIN_SUCCESS:
                    // Np-op, handled by sign-in check
                    mStatusText.setText(R.string.signed_in);
                    break;
            }
     
            if (user == null) {
                // Signed out
                mPhoneNumberViews.setVisibility(View.VISIBLE);
                mSignedInViews.setVisibility(View.GONE);
     
                mStatusText.setText(R.string.signed_out);;
            } else {
                // Signed in
                mPhoneNumberViews.setVisibility(View.GONE);
                /*
                mSignedInViews.setVisibility(View.VISIBLE);
     
                enableViews(mPhoneNumberField, mVerificationField);
                mPhoneNumberField.setText(null);
                mVerificationField.setText(null);
     
                mStatusText.setText(R.string.signed_in);
                mDetailText.setText(getString(R.string.firebase_status_fmt, user.getUid()));
                */
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
     
            }
        }
     
        private boolean validatePhoneNumber() {
            String phoneNumber = mPhoneNumberField.getText().toString();
            if (TextUtils.isEmpty(phoneNumber)) {
                mPhoneNumberField.setError("Invalid phone number.");
                //mPhoneNumberField.setTextColor(Color.parseColor("#ff1744"));
                return false;
            }
     
            return true;
        }
     
        private void enableViews(View... views) {
            for (View v : views) {
                v.setEnabled(true);
            }
        }
     
        private void disableViews(View... views) {
            for (View v : views) {
                v.setEnabled(false);
            }
        }
     
     
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.button_start_verification:
                    if (!validatePhoneNumber()) {
                        return;
                    }
     
                    ///////hide keyboard start
                    InputMethodManager inputManager = (InputMethodManager)
                            getSystemService(Context.INPUT_METHOD_SERVICE);
     
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    /////////hide keyboard end
     
     
                    //mStatusText.setText("Authenticating....!");
                    progressBar.setVisibility(View.VISIBLE);
                    startPhoneNumberVerification(mPhoneNumberField.getText().toString());
     
                    break;
                case R.id.button_verify_phone:
                    String code = mVerificationField.getText().toString();
                    if (TextUtils.isEmpty(code)) {
                        mVerificationField.setError("Cannot be empty.");
                        return;
                    }
     
                    verifyPhoneNumberWithCode(mVerificationId, code);
                    break;
                case R.id.button_resend:
                    resendVerificationCode(mPhoneNumberField.getText().toString(), mResendToken);
                    break;
                case R.id.sign_out_button:
                    signOut();
                    break;
            }
        }
    }

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Avril 2016
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Congo-Brazzaville

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Services à domicile

    Informations forums :
    Inscription : Avril 2016
    Messages : 9
    Points : 7
    Points
    7
    Par défaut
    excusez moi voila le code java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    package com.example.programmer.bmarketversion3;
     
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.support.annotation.NonNull;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.text.TextUtils;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.TextView;
     
    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;
     
    public class Login2 extends AppCompatActivity  implements
            View.OnClickListener {
     
        private static final String TAG = "PhoneAuthActivity";
     
        private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";
     
        private static final int STATE_INITIALIZED = 1;
        private static final int STATE_CODE_SENT = 2;
        private static final int STATE_VERIFY_FAILED = 3;
        private static final int STATE_VERIFY_SUCCESS = 4;
        private static final int STATE_SIGNIN_FAILED = 5;
        private static final int STATE_SIGNIN_SUCCESS = 6;
     
        // [START declare_auth]
        private FirebaseAuth mAuth;
        // [END declare_auth]
     
        private boolean mVerificationInProgress = false;
        private String mVerificationId;
        private PhoneAuthProvider.ForceResendingToken mResendToken;
        private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
     
        private ViewGroup mPhoneNumberViews;
        private ViewGroup mSignedInViews;
     
        private TextView mStatusText;
        private TextView mDetailText;
     
        private EditText mPhoneNumberField;
        private EditText mVerificationField;
     
        private Button mStartButton;
        private Button mVerifyButton;
        private Button mResendButton;
        private Button mSignOutButton;
     
        ProgressBar progressBar;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login2);
     
            if (savedInstanceState != null) {
                onRestoreInstanceState(savedInstanceState);
            }
     
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
     
            // Assign views
            mPhoneNumberViews = (ViewGroup) findViewById(R.id.phone_auth_fields);
            mSignedInViews = (ViewGroup) findViewById(R.id.signed_in_buttons);
     
            mStatusText = (TextView) findViewById(R.id.status);
            mDetailText = (TextView) findViewById(R.id.detail);
     
            mPhoneNumberField = (EditText) findViewById(R.id.field_phone_number);
            mVerificationField = (EditText) findViewById(R.id.field_verification_code);
     
            mStartButton = (Button) findViewById(R.id.button_start_verification);
            mVerifyButton = (Button) findViewById(R.id.button_verify_phone);
            mResendButton = (Button) findViewById(R.id.button_resend);
            mSignOutButton = (Button) findViewById(R.id.sign_out_button);
     
            // Assign click listeners
            mStartButton.setOnClickListener(this);
            mVerifyButton.setOnClickListener(this);
            mResendButton.setOnClickListener(this);
            mSignOutButton.setOnClickListener(this);
     
            // [START initialize_auth]
            mAuth = FirebaseAuth.getInstance();
            // [END initialize_auth]
     
            // Initialize phone auth callbacks
            // [START phone_auth_callbacks]
            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 verificaiton without
                    //     user action.
                    Log.d(TAG, "onVerificationCompleted:" + credential);
                    // [START_EXCLUDE silent]
                    mVerificationInProgress = false;
                    // [END_EXCLUDE]
     
                    // [START_EXCLUDE silent]
                    // Update the UI and attempt sign in with the phone credential
                    updateUI(STATE_VERIFY_SUCCESS, credential);
                    // [END_EXCLUDE]
                    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);
                    // [START_EXCLUDE silent]
                    mVerificationInProgress = false;
                    // [END_EXCLUDE]
     
                    if (e instanceof FirebaseAuthInvalidCredentialsException) {
                        // Invalid request
                        // [START_EXCLUDE]
                        mPhoneNumberField.setError("Invalid phone number.");
                        // [END_EXCLUDE]
                    } else if (e instanceof FirebaseTooManyRequestsException) {
                        // The SMS quota for the project has been exceeded
                        // [START_EXCLUDE]
                        Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
                                Snackbar.LENGTH_SHORT).show();
                        // [END_EXCLUDE]
                    }
     
                    // Show a message and update the UI
                    // [START_EXCLUDE]
                    updateUI(STATE_VERIFY_FAILED);
                    // [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);
     
                    // Save verification ID and resending token so we can use them later
                    mVerificationId = verificationId;
                    mResendToken = token;
     
                    // [START_EXCLUDE]
                    // Update UI
                    updateUI(STATE_CODE_SENT);
                    // [END_EXCLUDE]
                }
            };
            // [END phone_auth_callbacks]
        }
     
        // [START on_start_check_user]
        @Override
        public void onStart() {
            super.onStart();
            // Check if user is signed in (non-null) and update UI accordingly.
            FirebaseUser currentUser = mAuth.getCurrentUser();
            updateUI(currentUser);
     
            // [START_EXCLUDE]
            if (mVerificationInProgress && validatePhoneNumber()) {
                startPhoneNumberVerification(mPhoneNumberField.getText().toString());
            }
            // [END_EXCLUDE]
        }
        // [END on_start_check_user]
     
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putBoolean(KEY_VERIFY_IN_PROGRESS, mVerificationInProgress);
        }
     
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            mVerificationInProgress = savedInstanceState.getBoolean(KEY_VERIFY_IN_PROGRESS);
        }
     
     
        private void startPhoneNumberVerification(String phoneNumber) {
            // [START start_phone_auth]
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                    phoneNumber,        // Phone number to verify
                    60,                 // Timeout duration
                    TimeUnit.SECONDS,   // Unit of timeout
                    this,               // Activity (for callback binding)
                    mCallbacks);        // OnVerificationStateChangedCallbacks
            // [END start_phone_auth]
     
            mVerificationInProgress = true;
            mStatusText.setVisibility(View.INVISIBLE);
        }
     
        private void verifyPhoneNumberWithCode(String verificationId, String code) {
            // [START verify_with_code]
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
            // [END verify_with_code]
            signInWithPhoneAuthCredential(credential);
        }
     
        // [START resend_verification]
        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
        }
        // [END resend_verification]
     
        // [START sign_in_with_phone]
        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");
     
                                FirebaseUser user = task.getResult().getUser();
                                // [START_EXCLUDE]
                                updateUI(STATE_SIGNIN_SUCCESS, user);
                                // [END_EXCLUDE]
                            } 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
                                    // [START_EXCLUDE silent]
                                    mVerificationField.setError("Invalid code.");
                                    // [END_EXCLUDE]
                                }
                                // [START_EXCLUDE silent]
                                // Update UI
                                updateUI(STATE_SIGNIN_FAILED);
                                // [END_EXCLUDE]
                            }
                        }
                    });
        }
        // [END sign_in_with_phone]
     
        private void signOut() {
            mAuth.signOut();
            updateUI(STATE_INITIALIZED);
        }
     
        private void updateUI(int uiState) {
            updateUI(uiState, mAuth.getCurrentUser(), null);
        }
     
        private void updateUI(FirebaseUser user) {
            if (user != null) {
                updateUI(STATE_SIGNIN_SUCCESS, user);
            } else {
                updateUI(STATE_INITIALIZED);
            }
        }
     
        private void updateUI(int uiState, FirebaseUser user) {
            updateUI(uiState, user, null);
        }
     
        private void updateUI(int uiState, PhoneAuthCredential cred) {
            updateUI(uiState, null, cred);
        }
     
        private void updateUI(int uiState, FirebaseUser user, PhoneAuthCredential cred) {
            switch (uiState) {
                case STATE_INITIALIZED:
                    // Initialized state, show only the phone number field and start button
                    enableViews(mStartButton, mPhoneNumberField);
                    disableViews(mVerifyButton, mResendButton, mVerificationField);
                    mDetailText.setText(null);
                    break;
                case STATE_CODE_SENT:
                    // Code sent state, show the verification field, the
                    enableViews(mVerifyButton, mResendButton, mPhoneNumberField, mVerificationField);
                    disableViews(mStartButton);
                    mDetailText.setText(R.string.status_code_sent);
                    mDetailText.setTextColor(Color.parseColor("#43a047"));
                    break;
                case STATE_VERIFY_FAILED:
                    // Verification has failed, show all options
                    enableViews(mStartButton, mVerifyButton, mResendButton, mPhoneNumberField,
                            mVerificationField);
                    mDetailText.setText(R.string.status_verification_failed);
                    mDetailText.setTextColor(Color.parseColor("#dd2c00"));
                    progressBar.setVisibility(View.INVISIBLE);
                    break;
                case STATE_VERIFY_SUCCESS:
                    // Verification has succeeded, proceed to firebase sign in
                    disableViews(mStartButton, mVerifyButton, mResendButton, mPhoneNumberField,
                            mVerificationField);
                    mDetailText.setText("Verfication Sucessfull");
                    mDetailText.setTextColor(Color.parseColor("#43a047"));
                    progressBar.setVisibility(View.INVISIBLE);
     
                    // Set the verification text based on the credential
                    if (cred != null) {
                        if (cred.getSmsCode() != null) {
                            mVerificationField.setText(cred.getSmsCode());
                        } else {
                            mVerificationField.setText(R.string.instant_validation);
                            mVerificationField.setTextColor(Color.parseColor("#4bacb8"));
                        }
                    }
     
                    break;
                case STATE_SIGNIN_FAILED:
                    // No-op, handled by sign-in check
                    mDetailText.setText(R.string.status_sign_in_failed);
                    mDetailText.setTextColor(Color.parseColor("#dd2c00"));
                    progressBar.setVisibility(View.INVISIBLE);
                    break;
                case STATE_SIGNIN_SUCCESS:
                    // Np-op, handled by sign-in check
                    mStatusText.setText(R.string.signed_in);
                    break;
            }
     
            if (user == null) {
                // Signed out
                mPhoneNumberViews.setVisibility(View.VISIBLE);
                mSignedInViews.setVisibility(View.GONE);
     
                mStatusText.setText(R.string.signed_out);;
            } else {
                // Signed in
                mPhoneNumberViews.setVisibility(View.GONE);
                /*
                mSignedInViews.setVisibility(View.VISIBLE);
     
                enableViews(mPhoneNumberField, mVerificationField);
                mPhoneNumberField.setText(null);
                mVerificationField.setText(null);
     
                mStatusText.setText(R.string.signed_in);
                mDetailText.setText(getString(R.string.firebase_status_fmt, user.getUid()));
                */
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
     
            }
        }
     
        private boolean validatePhoneNumber() {
            String phoneNumber = mPhoneNumberField.getText().toString();
            if (TextUtils.isEmpty(phoneNumber)) {
                mPhoneNumberField.setError("Invalid phone number.");
                //mPhoneNumberField.setTextColor(Color.parseColor("#ff1744"));
                return false;
            }
     
            return true;
        }
     
        private void enableViews(View... views) {
            for (View v : views) {
                v.setEnabled(true);
            }
        }
     
        private void disableViews(View... views) {
            for (View v : views) {
                v.setEnabled(false);
            }
        }
     
     
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.button_start_verification:
                    if (!validatePhoneNumber()) {
                        return;
                    }
     
                    ///////hide keyboard start
                    InputMethodManager inputManager = (InputMethodManager)
                            getSystemService(Context.INPUT_METHOD_SERVICE);
     
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    /////////hide keyboard end
     
     
                    //mStatusText.setText("Authenticating....!");
                    progressBar.setVisibility(View.VISIBLE);
                    startPhoneNumberVerification(mPhoneNumberField.getText().toString());
     
                    break;
                case R.id.button_verify_phone:
                    String code = mVerificationField.getText().toString();
                    if (TextUtils.isEmpty(code)) {
                        mVerificationField.setError("Cannot be empty.");
                        return;
                    }
     
                    verifyPhoneNumberWithCode(mVerificationId, code);
                    break;
                case R.id.button_resend:
                    resendVerificationCode(mPhoneNumberField.getText().toString(), mResendToken);
                    break;
                case R.id.sign_out_button:
                    signOut();
                    break;
            }
        }
    }
    BUILD GRADLE
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    apply plugin: 'com.android.application'
     
    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "com.example.programmer.bmarketversion3"
            minSdkVersion 22
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            multiDexEnabled true
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        buildToolsVersion '26.0.2'
        dexOptions {
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
        productFlavors {
        }
    }
     
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'com.android.support:appcompat-v7:27.1.0'
        implementation 'com.android.support:cardview-v7:27.1.0'
        implementation 'com.android.support:recyclerview-v7:27.1.0'
        implementation 'com.weiwangcn.betterspinner:library-material:1.1.0'
        implementation 'com.android.support:design:27.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        implementation 'com.android.support:support-v4:27.0.0-beta1'
        implementation 'com.miguelcatalan:materialsearchview:1.4.0'
        implementation 'com.android.support:support-vector-drawable:27.0.0-beta1'
        implementation 'com.google.firebase:firebase-core:16.0.4'
     
     
        implementation 'com.google.firebase:firebase-messaging:17.3.3'
        implementation 'com.google.firebase:firebase-auth:16.0.4'
     
        implementation 'com.roughike:bottom-bar:2.3.1'
       // implementation 'com.firebaseui:firebase-ui:0.6.2'
        // implementation 'com.google.firebase:firebase-auth:16.0.4'
        //implementation 'com.facebook.android:facebook-login:[4,5)'
     
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }
    apply plugin: 'com.google.gms.google-services';

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Avril 2016
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Congo-Brazzaville

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Services à domicile

    Informations forums :
    Inscription : Avril 2016
    Messages : 9
    Points : 7
    Points
    7
    Par défaut
    J ai trouve la solution,le problème était dans le code indicatif du pays ou je suis

Discussions similaires

  1. Supprimer le fait que mon ordi cherche un proxy
    Par Stargate SG1 dans le forum Internet
    Réponses: 3
    Dernier message: 22/01/2008, 15h27
  2. Ouverture formulaire ne se fait que sur un enregistrement..?
    Par heavenvibes dans le forum VBA Access
    Réponses: 5
    Dernier message: 13/11/2007, 15h23
  3. [MySQL] While qui ne fait que boucler
    Par helene38250 dans le forum PHP & Base de données
    Réponses: 8
    Dernier message: 08/08/2007, 12h29
  4. Réponses: 5
    Dernier message: 15/05/2007, 22h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo