Need Help in Checking Internet Connection in Android?
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 Connectio开发者_StackOverflow社区n 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)
Add the android.permission.ACCESS_NETWORK_STATE
permission request to your application manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
精彩评论