How to programmatically check availibilty of internet connection in Android?

后端 未结 8 1920
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 04:02

I want to check programmatically whether there is an internet connection in Android phone/emulator. So that once I am sure that an internet connection is present then I\'ll

8条回答
  •  醉梦人生
    2020-12-09 04:23

    Try this:

    ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    
    if(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {
       text.setText("hey your online!!!")     ;               
       //Do something in here when we are connected   
    } else if(connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED   ) {
       text.setText("Look your not online");           
    }
    

提交回复
热议问题