开发者

How do you check the internet connection in android? [duplicate]

This question already has answers here: How to check internet access on Android? InetAddress never 开发者_开发知识库times out (64 answers) Closed 6 years ago.

I want to check the internet connectivity in each activity. If it is lost a message should be displayed.

Can any one guide me how to achieve this?


Only one connection can be active at any one point. So a simpler answer is:

final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
    // notify user you are online
} else {
    // notify user you are not online
} 

It also caters for any new type of network such as ConnectivityManager#TYPE_WIMAX


Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


You can use the ConnectivityManager to check the network state.

ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if ( conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED 
    || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ) {

    // notify user you are online

}
else if ( conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED 
    || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) {

    // notify user you are not online
}

Note that the constants ConnectivityManager.TYPE_MOBILE and ConnectivityManager.TYPE_WIFI represent connection types and these two values are not exhaustive. See here for an exhaustive list.


Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


You can do this, for various types of network status

public void  checkNetworkStatus(){

    final ConnectivityManager connMgr = (ConnectivityManager)
     this.getSystemService(Context.CONNECTIVITY_SERVICE);

     final android.net.NetworkInfo wifi =
     connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

     final android.net.NetworkInfo mobile =
     connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

     if( wifi.isAvailable() ){

     Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
     }
     else if( mobile.isAvailable() ){

     Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
     }
     else
     {

         Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
     }

}


You can check network coverage and data availability of Mobile and wi-fi directly with

For Network coverage availability,

private boolean isNetworkAvailable()
{
 ConnectivityManager conxMgr = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

 NetworkInfo mobileNwInfo = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
 NetworkInfo wifiNwInfo   = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

return ((mobileNwInfo== null ? false : mobileNwInfo.isAvailable()) || (wifiNwInfo == null ? false : wifiNwInfo.isAvailable()));

}

For Data availability if network available

private boolean isDataAvailable()
{
  ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

 NetworkInfo mobileNwInfo = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
 NetworkInfo wifiNwInfo   = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

return ((mobileNwInfo== null? false : mobileNwInfo.isConnected() )|| (wifiNwInfo == null? false : wifiNwInfo.isConnected()));
}


Correction

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
    ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {
   //notify user you are online
}

should be

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
    ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
    //notify user you are online
}


Register a broadcast receiver to handle CONNECTIVITY_ACTION. See this full example. You should update a static variable 'connectionAvailable' that will be accessible everywhere and everytime through its respective getter.

Remember to declare the broadcast receiver in the manifest file:

<receiver android:name=".NetworkConnectivityReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
    </intent-filter>
</receiver>

On the matter of 'checking in each activity', may be you would be interested in using a BaseActivity extended by your activities and managing the test of connectivity and displaying the message.

Also, note that using events (not polling from activities) will be more efficient.


You are not using ConnectivityManager.getNetworkInfo(0).getState() and ConnectivityManager.getNetworkInfo(1).getState() properly, instead of hardcoding the values (1) and (0) use ConnectivityManager.TYPE_WIFI and ConnectivityManager.TYPE_MOBILE


This is a boolean check to see if you have network access. It doesn't determine what kind of network access - mobile, wifi..., it just checks to see if you're online.

boolean mobileNwInfo = false;  
ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);  
try { mobileNwInfo = conxMgr.getActiveNetworkInfo().isConnected(); }  
catch (NullPointerException e) { mobileNwInfo = false; }  
if ( mobileNwInfo == false ) {
  // Your code goes here...
}  
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜