Reputation: 35
I am new to Android and when I give the following snippet, my Android application is crashing.
ConnectivityManager manager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); // Application is crashing in this line.
I also checked the AndroidManifest.xml where I gave the Internet permission to the application. Any help would be really useful for my further work.
Upvotes: 3
Views: 14362
Reputation: 33996
I am not sure what you want to say but if you want to check internet connection then you can use this code
/**
* THIS IS FUNCTION FOR CHECKING INTERNET CONNECTION
* @return TRUE IF INTERNET IS PRESENT ELSE RETURN FALSE
*/
public boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return (activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected());
}
and dont forget
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
Upvotes: 9
Reputation: 16196
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
Upvotes: 0