Android detects if the Internet is connected

The apps developed today do not need to be networked, so it is especially important to accurately control the current network state. There are often such scenarios as to get the current network type of the phone, whether Wi-Fi is connected, and whether it can access the Internet (Internet). Today we are talking about how to determine if the phone can communicate with the server.

Note: All the code in this article is based on the kotlin language implementation.

Error method

Before writing the code, I searched the mainstream solution on the Internet and found that everyone is the same idea in the first part: use a state in NetworkInfo (State) to determine whether it is a connection state, but the result is cruel, the method is Used to determine if Wi-Fi is connected, it does not mean that the phone is connected to the Internet. But I also posted the code for your reference:

2. The correct method

Permissions:

Solution 1: Ping server address (simple)

Through the return result of the ping command to know whether to connect to the external network, now the mainstream effective methods are this principle, but most of them are ping Baidu addresses, here is recommended to use 8.8.8.8, the sample code is as follows:

PS: 8.8.8.8 is a dns server address, which is the IP address of the free DNS server provided by Google.
The advantages of this program are:

Can run on the main thread and is synchronized

high speed

The disadvantages are also:

It doesn't work on some old phones, such as the Galaxy S3. The reason is related to the OEM of the mobile phone. For specific reasons, please refer to: Why does ping works on some devices and not others?

Option 2: Connect to the server port (recommended)

Use a socket to connect to a port on the server, and use the result of the connection to determine whether the phone is connected to the external network. This method is recommended because this method is applicable to all mobile phones Android version. The sample code is as follows:

funisOnline() =try{valTImeoutMs =1500Socket().use{//The ports connected are different and the network protocol is different. DNS:53;HTTP:80valsocketAddr = InetSocketAddress("8.8.8.8",53) it.connect(socketAddr, TImeoutMs) }true} catch (e: Throwable) {false}

Advantages :

Fast, very fast

Method to eat, no annoying version adaptation

Disadvantages :

Cannot run in the main thread

Option 3: Obtain IP based on domain name (recommended)

Use the method getByName() provided by InetAddress to obtain the IP address of a domain name, and then judge whether to connect to the external network according to whether the acquisition is successful. This method is recommended.

Advantages :

Fast, timeout can be customized

Can run on the main thread

All versions are eaten

Disadvantages:

No

3. Summary

In fact, the ideas of the above three effective programs are the same:

Make a network access (you must access an address on the public network, whether it is a domain name or an IP address)

Actual judgment based on the results returned by the network

PD Fast Charger

Pd Fast Charger,45W Usb-C Charger,Mobile Phone Fast Charger Usb-C,Apple Phone 12 Charger

Guangdong Mingxin Power Technologies Co.,Ltd. , https://www.mxpowersupply.com