Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Establish internet connection

Tags:

android

I'm surprised I cannot find any info on the internet on this common situation: How can I start an internet connection? I looked at ConnectivityManager but it seems it is just for monitoring network connectivity.

PS: The phone will be rooted, so it is not a problem.

like image 893
Caner Avatar asked Oct 28 '11 10:10

Caner


2 Answers

If you want simply enable WiFi you can use this method:

private boolean changeWifiState(boolean state) {
    final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifi.setWifiEnabled(state);
    return wifi.isWifiEnabled();
}

Check the permissions required for this. I think you should add these permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
like image 55
Yury Avatar answered Oct 20 '22 09:10

Yury


You can indeed turn wifi on or off (see also this article) but there is no guarantee that if wifi is turned on there will be an internet connection.

The ConnectivityManager only allows you to inspect the current connectivity state. You cannot use it to enable a connection. Also the ConnectivityManager has no knowledge if an active network connection is an internet connection, but it is easy to check this yourself (see this post for example).

like image 26
THelper Avatar answered Oct 20 '22 09:10

THelper