任何能检查android手机中是否有互联网连接的功能?

时间:2011-06-13 08:17:03

标签: android

我正在尝试制作一个能够检查互联网是否在Android手机上启用的功能。请帮忙..

5 个答案:

答案 0 :(得分:2)

使用此:

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

答案 1 :(得分:2)

请检查功能......

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

答案 2 :(得分:1)

这会对你有帮助..

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

答案 3 :(得分:0)

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

希望这有帮助!

答案 4 :(得分:-1)

这将返回图像的位图

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

将位图设置为ImageView

imageView.setImageBitmap(result);