开发者

Any function that will check if there is internet connectivity in android mobile?

I am开发者_如何学JAVA trying to make a function that will check if internet is on in android mobile. Please help ..


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;         
}


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;
    }


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;
    }
}


private boolean haveInternet() {
        NetworkInfo info = ((ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (info == null || !info.isConnected()) {
            return false;
        }
        return true;
    }

Hope this helps!


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜