views:

526

answers:

1

dear friends,

i want to check internet connectivity in each activity if lost then it should display a message.

can any one guide me how to achieve this?

any help would be appriciated.

+3  A: 

You can use the ConnectivityManager to check the network state.

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

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
    ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {


    //notify user you are online

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

} 
Dan
it is giving me following error ConnectivityService: Neither user 10026 nor current process has android.permission.ACCESS_NETWORK_STATE.on conMgr.getNetworkInfo
UMMA
You have to add this line to your AndroidManifest.xml:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
Sarp Centel