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.
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"/>
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With