tags:

views:

89

answers:

1

Hai Friends, I want to display a toast message to the user, when there is poor or no internet connection, so i have written the following code in my activity.

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

      Toast.makeText(getBaseContext(),"Please

Check Your Internet Connection and Try Again",Toast.LENGTH_SHORT).show();

  }

The error message in Log is

10-25 15:52:03.412: ERROR/AndroidRuntime(19084): Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10031 nor current process has android.permission.ACCESS_NETWORK_STATE.
10-25 15:52:03.412: ERROR/AndroidRuntime(19084):     at android.os.Parcel.readException(Parcel.java:1218)
10-25 15:52:03.412: ERROR/AndroidRuntime(19084):     at android.os.Parcel.readException(Parcel.java:1206)
10-25 15:52:03.412: ERROR/AndroidRuntime(19084):     at android.net.IConnectivityManager$Stub$Proxy.getNetworkInfo(IConnectivityManager.java:266)
10-25 15:52:03.412: ERROR/AndroidRuntime(19084):     at android.net.ConnectivityManager.getNetworkInfo(ConnectivityManager.java:153)
+2  A: 

Add the android.permission.ACCESS_NETWORK_STATE permission request to your application manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Veeti
@ Mr.Veeti: Can u please tell me how to detect the interconnection is poor, becuase i have written if the net is disconnected only
Tilsan The Fighter
@Tilsan: define poor interconnection
Kennet
@Kennet: Can u please tell little bit explanation, how to define poor internet connection
Tilsan The Fighter
@Tilsan: My question was, What do mean with "poor interconnection"? Now you say internet connection, okay, what is that? Is that slow connection, low bitrate when transfering files? Intermittent connection, when you loose contact. Something else?
Kennet