Android internet connectivity check problem

后端 未结 5 1461
借酒劲吻你
借酒劲吻你 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;
               }
            }
    

    }

提交回复
热议问题