-1

I am working on an application that has OTP screen integrated with it. There is one minor issue there. I have to check for two conditions, one is, if the network is not available then I have to display an error message that Network not available. Else if user has entered a wrong OTP then then error message should say that 'Wrong OTP entered'.

Right now I am displaying a common message for both cases. No if else. I want to separate them into two. My problem is how do we check for Mobile network issue, if its available or not?

if (task.isSuccessful()) {
  if (previousScreenTitle == R.string.login) {
    userPrivateInfo = Constants.ALL_USERS_REFERENCE.child(userMobileNumber);
    userPrivateInfo.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        /* Check if User mobile number is found in database */
        if (dataSnapshot.exists()) {
          startActivity(new Intent(OTP.this, NammaApartmentsHome.class));
        }
        /* User record was not found in firebase hence we navigate them to Sign Up page*/
        else {
          Intent intent = new Intent(OTP.this, SignUp.class);
          intent.putExtra(Constants.MOBILE_NUMBER, userMobileNumber);
          startActivity(intent);
        }
        finish();
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
    });
  } else {
    setResult(Activity.RESULT_OK, new Intent());
    finish();
  }
} else {
  textResendOTPOrVerificationMessage.setText(R.string.check_network_connection);
}
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Ashish Jha
  • 173
  • 1
  • 3
  • 18
  • Possible duplicate of [How to check internet access on Android? InetAddress never times out](https://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-times-out) – Dima Kozhevin Jul 07 '18 at 08:57
  • Also you might like [Check Network and Internet Connection - Android](https://stackoverflow.com/q/31283443/3166697) or [Check for Active internet connection Android](https://stackoverflow.com/q/17717749/3166697) or [How to programmatically check availibilty of internet connection in Android?](https://stackoverflow.com/q/4530846/3166697) – Dima Kozhevin Jul 07 '18 at 09:03

2 Answers2

-1

Use this method

private boolean chechInternetConnection() {
    connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
                connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
            connected = true;
            Log.i("Internet", "Connected");
        } else {
            connected = false;
            Log.i("Internet", "Not Connected");
        }
    }
    return connected;
}
Brijesh Joshi
  • 1,817
  • 1
  • 9
  • 24
-1

The following snippet shows how to use the ConnectivityManager to query the active network and determine if it has Internet connectivity.

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

Determine the type of your internet connection

Device connectivity can be provided by mobile data, WiMAX, Wi-Fi, and ethernet connections. By querying the type of the active network, as shown below, you can alter your refresh rate based on the bandwidth available.

boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

Check Official Documentation for detailed understanding.

Hope this helps.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20