Android: establish an internet connection

I am surprised that I cannot find any information on the Internet about this general situation: how can I start an Internet connection? I looked at the ConnectivityManager , but it seems to be easy to monitor network connectivity.

PS: the phone will be rooted, so this is not a problem.

+6
source share
4 answers

I found the following solution:

1) Add your preferred Wi-Fi / APN configuration (access point name) to your phone settings 2) Enable Wi-Fi / APN
3) The connection will be established automatically

Wifi configuration is simple and the next page shows how to do it:

How to programmatically create and read WEP / EAP WiFi configurations in Android?

APN configuration is complicated. Android has a hidden API that allows you to access / change these parameters, and for this you need to use Reflection . Of course, this could happen with future versions of Android. The next page describes how to access these settings / change the following parameters:

http://blogs.msdn.com/b/zhengpei/archive/2009/10/13/managing-apn-data-in-google-android.aspx

+1
source

If you just want to 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"/> 
+2
source

You can enable or disable Wi-Fi (see also in this article ) but there is no guarantee that if Wi-Fi is turned on, there will be an Internet connection.

ConnectivityManager allows you to check the current status of a connection. You cannot use it to enable a connection. Also, the ConnectivityManager does not know if the active network connection is an Internet connection, but it is easy to check (see this post for example).

+1
source

Sockets are what you need. To get basic information about them, read here: http://en.wikipedia.org/wiki/Internet_socket You do not need to read everything, but I think it is important that you read at least all the "implementation problems" in order to get familiar with socket methods. Of course, these methods are implemented differently for each platform (Windows, Android ...), but they usually do the same everywhere . So, as soon as you understand what each of them is doing, you can easily work with sockets on any platform.

This diagram (from the wiki) helps demonstrate the use of sockets: enter image description here

You are on the client side. Therefore, just create a socket and call the connect method for the IP address of your server.

Personally, I have never used sockets in Android development. But I think you should use this class: http://developer.android.com/reference/java/net/Socket.html

0
source

Source: https://habr.com/ru/post/900274/


All Articles