• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Check network connection

Hi all

I'm new to here and i hope i can get some help. I not searched the forums for similar topics cause i do not have much time, so please forgive me if this question asked before.

So, I want to check that the network connection is alive on the phone from program but when i try to run this code
Code:
private boolean isOnline() {
	    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    return false;
	}

I get the message "Sorry 'Appname' is stopped unexpectedly. Try again"
Can someone help me with this?

EDIT1

I might have to search the solution elsewhere, i added permissions for network, run the application and got the same error but when i tried to start it from the menu it said application not installed.

Thank you

iwanabeguru
 
You need to add the following lines to your manifest:
Code:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
 
Back
Top Bottom