Get the App
SLTechnology News&Howtos  ›  Development  › 

How to judge whether the current device is connected to the Internet in Android

Shulou Source: shulou.com Published: 2022-06-01 00:18:51 10月05日 Update

This article mainly introduces the relevant knowledge of how to judge whether the current equipment is in the network state in Android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Android article on how to judge whether the current device is in the network state. Let's take a look.

First of all, to find out whether the current android device is connected or not, android itself provides us with a service.

PrivateConnectivityManager connectivityManager;// is used to determine whether there is a network connectivityManager = (ConnectivityManager) getActivity (). GetSystemService (Context.CONNECTIVITY_SERVICE); / / get the connection service of the current network NetworkInfo info = connectivityManager.getActiveNetworkInfo (); / / obtain the active network connection information

First of all, we have to get the android network service and add a permission to the configuration file.

Then we get the service of the system through Context.getSystemService (Context.CONNECTIVITY_SERVICE).

Then we use the NetworkInfo class provided to us by android to host the system and return the network service information to us.

If (info = = null) {/ / currently there is no active network connection (indicating that the user has turned off the data traffic service and has not enabled other data services such as WiFi) Toast.makeText (getActivity (), "check whether the network connection is open", Toast.LENGTH_SHORT) .network () } else {/ / currently has an active network connection}

Of course, we can also use this code to determine whether our network connection is available.

/ / to return whether it is valid. If True, it means that the current Android phone is connected to the Internet, which may be WiFi or GPRS, HSDPA and so on. Specifically, you can judge the detailed access method connectivityManager.getActiveNetworkInfo (). IsAvailable () through the getActiveNetworkInfo () method of the ConnectivityManager class; this is the end of the article on "how to determine whether the current device is connected to the network in Android". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to judge whether the current equipment is connected or not in Android". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Network service status device knowledge information content data activation article system network service validity code value available through mobile phone easy to operate file article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Shulou Information Apple Xiaomi