0

I am trying to make a function that will check if internet is on in android mobile. Please help ..

Kruti Mevada
  • 517
  • 5
  • 7
  • 21
  • Also take a look at this to see that you are connected via wifi/mobile: http://stackoverflow.com/a/6357668/198348 - Though this ensures that the network connection is up, not if the internet itself is up. – Ehtesh Choudhury Aug 17 '12 at 15:42

5 Answers5

2

Use this:

public boolean IsNetConnected() 
{
    boolean NetConnected = false;
    try
    {
        ConnectivityManager connectivity = (ConnectivityManager)ClassContext.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity == null) 
          {
              Log.w("tag", "couldn't get connectivity manager");
              NetConnected = false;
          } 
          else 
          {
             NetworkInfo[] info = connectivity.getAllNetworkInfo();
             if (info != null) 
             {
                for (int i = 0; i < info.length; i++) 
                {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) 
                    {
                        NetConnected = true;
                    }
                 }
             }
         }
    }
    catch (Exception e) {
        // TODO: handle exception
        NetConnected = false;
    }
    return NetConnected;         
}
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30
2

Please checkout the function...

public boolean checkNetworkRechability() {
        Boolean bNetwork = false;
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        for (NetworkInfo networkInfo : connectivityManager.getAllNetworkInfo()) {
            int netType = networkInfo.getType();
            int netSubType = networkInfo.getSubtype();

            if (netType == ConnectivityManager.TYPE_WIFI) {
                bNetwork = networkInfo.isConnected();
                if (bNetwork == true)
                    break;
            } else if (netType == ConnectivityManager.TYPE_MOBILE
                    && netSubType != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
                bNetwork = networkInfo.isConnected();
                if (bNetwork == true)
                    break;
            } else {
                bNetwork = false;
            }
        }
        if (!bNetwork) {
            Toast.makeText(getApplicationContext(),"You are not in network",Toast.LENGTH_SHORT).show();
        }
        return bNetwork;
    }
Vishal Khakhkhar
  • 2,106
  • 3
  • 29
  • 60
1

This will help you..

 private boolean netCheckin() {

    try {

        ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        nInfo.getActiveNetworkInfo().isConnectedOrConnecting();

        Log.d(tag, "Net avail:"
                + nInfo.getActiveNetworkInfo().isConnectedOrConnecting());

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            Log.d(tag, "Network available:true");
            return true;
        } else {
            Log.d(tag, "Network available:false");
            return false;
        }

    } catch (Exception e) {
        return false;
    }
}
Sujit
  • 10,512
  • 9
  • 40
  • 45
0
private boolean haveInternet() {
        NetworkInfo info = ((ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (info == null || !info.isConnected()) {
            return false;
        }
        return true;
    }

Hope this helps!

Egor
  • 39,695
  • 10
  • 113
  • 130
-1

This will return Bitmap of Image

private Bitmap download_Image(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {

        }
        return bm;
    }

Set Bitmap to ImageView

imageView.setImageBitmap(result);
Vishal Khakhkhar
  • 2,106
  • 3
  • 29
  • 60