To check wifi or 3G exist in android
Hello In my android application i am using the below code to make sure that network exists.
But am receiving error at the very first line.Do i need to check any permission other than those listed.
public static boolean haveInternet(Context ctx) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to
// disable internet while roami开发者_C百科ng, just return false
return false;
}
return true;
Permissions in manifest
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name= "android.permission.CHANGE_NETWORK_STATE"></uses-permission>
I am using
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
instead of
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
Try changing it.
精彩评论