Android internet connectivity check problem

后端 未结 5 1436
借酒劲吻你
借酒劲吻你 2020-11-28 14:06

I\'m new to Android development and working on an Android application that requires the phone to be connected to the internet, through either Wifi, EDGE or 3G.

This

相关标签:
5条回答
  • 2020-11-28 14:46

    To check internet is there or not can be checked only on device......On emulator it may not work.... I have got the following code & its working 100% on android device..... :)

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        tv = (TextView)findViewById(R.id.txt);
        b = checkInternetConnection();
    
    
        if(b!=true)
        {
            tv.setText("net is not dr.......");
        }
        else
        {
            tv.setText("net is dr.......");
        }
    
    }
    //Check weather Internet connection is available or not
    public boolean checkInternetConnection() {
               final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
               if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() &&    conMgr.getActiveNetworkInfo().isConnected()) {
                     return true;
               } else {
                     System.out.println("Internet Connection Not Present");
                   return false;
               }
            }
    

    }

    0 讨论(0)
  • 2020-11-28 14:53

    You have used this snippet.

    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;
                      }
    
          }
    
    0 讨论(0)
  • 2020-11-28 15:03

    If the crash is directly on your line:

    return cm.getActiveNetworkInfo().isConnectedOrConnecting();
    

    then that means getActiveNetworkInfo() returned null, because there is no active network -- in that case, your isConnected() method should return false.

    0 讨论(0)
  • 2020-11-28 15:04

    I wrote this method to handle this:

    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni!=null && ni.isAvailable() && ni.isConnected()) {
            return true;
        } else {
            return false; 
        }
    }
    

    One way to do it I guess...

    0 讨论(0)
  • 2020-11-28 15:08

    use this to detemine if connceted to wifi/3g:

    is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
        isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
        network = is3g||isWifi;
    

    and this to enable wifi yourself:

    WifiManager wifiManager = (WifiManager) MainWindowYuval.this.getSystemService(Context.WIFI_SERVICE);
                        wifiManager.setWifiEnabled(true);      
    
    0 讨论(0)
提交回复
热议问题