-4

how can I check my device is connected with internet or not.

I have android device , in that I start wifi data but in that wifi data is for local network but still it show your internet connection is on while actually is not.

then how can I check intenert conncetion class this class throughI check
I'll give you code for internet conncetion check

public class NetworkUtil {

    public static int TYPE_WIFI = 1;

    public static int TYPE_MOBILE = 2;

    public static int TYPE_NOT_CONNECTED = 0;

    public static int getConnectivityStatus(Context context) {

        if (Build.VERSION.SDK_INT >= 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {

            return TYPE_WIFI;

        } else {

            return TYPE_NOT_CONNECTED;
        }

    }

    public static String getConnectivityStatusString(Context context) {
        int conn = NetworkUtil.getConnectivityStatus(context);
        String status = null;
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = "Wifi enabled";
        } else if (conn == NetworkUtil.TYPE_MOBILE) {
            status = "Mobile data enabled";
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
        }
        return status;
    }

    public static boolean isInternetWorking() {
        boolean success = false;
        try {
            URL url = new URL("https://google.com");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(10000);
            connection.connect();
            success = connection.getResponseCode() == 200;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }
} 
Shruti
  • 775
  • 9
  • 25
Radhika
  • 13
  • 6

5 Answers5

0

Kindly use following method to check internet connection.

private boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null;
}
Yahya Mukhtar
  • 466
  • 5
  • 13
  • thank you for ans but it's only check wifi connectivity i need when actually internet is working or not – Radhika Feb 28 '18 at 05:46
0

0 means no internet 1 means internet

private int checkInternetConnectivity() {
        ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
        if (netInfo == null) {
            return 0;
        } else {
            return 1;
        }
    }
007
  • 486
  • 5
  • 15
  • thank you for ans but it's only check wifi connectivity i need when actually internet is working or not – Radhika Feb 28 '18 at 05:47
0

Use this way in class

     NetworkUtil networkUtil=new NetworkUtil(this)
     if(networkUtil.isInternetWorking()){
     Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
     }else{
     Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show();
     }
mehul chauhan
  • 1,661
  • 9
  • 19
0

You can easily check Internet Connection by this code:

ConnectivityManager ConnectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = ConnectionManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected() ) {

             //Connected
        } else {
            //Not connected
        }

Don't forget to declare permission in Manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
AskNilesh
  • 58,437
  • 15
  • 99
  • 129
SahdevRajput74
  • 742
  • 6
  • 18
0

Make A Class:-

public class ConnctionDetector 

{

ConnectivityManager connectivityManager;

Context context;

NetworkInfo info;

public ConnctionDetector(Context context)
{
    this.context=context;
}
public boolean checkin(Context context)
{
    boolean flag = false;
    try {
        connectivityManager = (ConnectivityManager) 
context.getSystemService(Context.CONNECTIVITY_SERVICE);
        info = connectivityManager.getActiveNetworkInfo();

        if (info.getType() == ConnectivityManager.TYPE_WIFI)
        {
            System.out.println(info.getTypeName());
            flag = true;
        }
        if (info.getType() == ConnectivityManager.TYPE_MOBILE)
        {
            System.out.println(info.getTypeName());
            flag = true;
        }
    } catch (Exception exception) {
        System.out.println("Exception at network connection....."
                + exception);
    }
    return flag;
}
}

And How TO Check:-

boolean checkconnection = new 
ConnctionDetector(getApplicationContext()).checkin(getApplicationContext());

if (!checkconnection) {
                Toast.makeText(getApplicationContext(), "No Internet 
Connection", Toast.LENGTH_SHORT).show();
}
mehul chauhan
  • 1,661
  • 9
  • 19
Ravish Sharma
  • 209
  • 2
  • 13