2016年3月2日 星期三

How to check the connection state:

How to check the connection state:


1.get the permission:
<!--Permission to check the connection state-->
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

2.get the ConnectivityManager
Context.getSystemService(Context.CONNECTIVITY_SERVICE).

3.check
ConnectivityManager cm =
       (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                     activeNetwork.isConnectedOrConnecting();