-1

I have recently started learning to code in android studio and have only basic knowledge of codding, I'm using Navigation drawer activity for my app which uses webView to show pages on the list of elements in the drawer,can anyone help me how do I implement a continuously Internet check method in it. Thank You.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Possible duplicate of [Android check internet connection](https://stackoverflow.com/questions/9570237/android-check-internet-connection) – 5ec20ab0 Aug 28 '18 at 11:27
  • https://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app follow this question. – PRATEEK BHARDWAJ Aug 28 '18 at 11:27

3 Answers3

0

To find out if the Internet operator used this method, its returns will return if they are connected

   public static boolean isConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    assert manager != null;
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}

Also you need one permission in Manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5ec20ab0
  • 742
  • 6
  • 15
0

The above given answer is only partially correct because the question states that it continuously want to check whether internet is present or not (Network changes). This is done by using broadcast receiver, for the code you can refer this question here.

shashank chandak
  • 544
  • 5
  • 13
0

This code will make a request to the Google DNS servers and check whether your device has Internet Access or not:

public static boolean isOnline(Context context) {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    }
    catch (IOException e)          { e.printStackTrace(); }
    catch (InterruptedException e) { e.printStackTrace(); }
    return false;
}
}

//This is used to show an Alert Box
public AlertDialog.Builder buildDialog(Context c) {

    AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("No Internet Connection");
    builder.setMessage("Seems like your Internet connection is down. Please Check");

    builder.setPositiveButton("OK", (dialog, which) -> {
        dialog.dismiss();
        finish();
    });

    return builder;
}
  • Thank you so much for your response but i want a view like google play store, when there is no internet connection it show a dialogue for it and a retry button with it. – Milan Joshi Aug 31 '18 at 11:50
  • For that all you have to do is to check for Internet connectivity the way @5ec20ab0 has said and then if there is no internet connection, pass an intent and call the No internet Connectivity layout. Hope this helps –  Aug 31 '18 at 13:17