1

I want to find is this mobile connected with internet or not(wifi and mobile data) iam using below code its working fine in all device except one mi note 4 i find an issue its not showing false when its not coonected with internet please help me.

  public class ConnectionDetector {
  private Context _context;

  public ConnectionDetector(Context context)
  {
    this._context = context;
  }

public boolean isConnectingToInternet()
{
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity != null)
    {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
        }
    }
    return false;
}
}

ConnectionDetector cd = new ConnectionDetector(ViewOrder.this);
Boolean isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
           // its giving true when its in offline only in mi note 4 device. all the mobiles which i checked its working fine
}
else {
}
sai android
  • 139
  • 1
  • 12

1 Answers1

0

Try this,

public class NetworkUtility {

/**
 * Provides whether internet is connected or not
 * @param context
 * @return true if connected , 
 * else false
 */
public static boolean isInternetOn(Context context) {
    if (context == null) 
        return false;

        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = conn.getActiveNetworkInfo();
        if (networkInfo != null) {
            return networkInfo.isConnected();
        }

    return false;
}
}
hasan_shaikh
  • 1,434
  • 1
  • 15
  • 38