Android monitoring network state changes and actual combat example code

We usually deal with the request error. Generally speaking, it is mainly divided into two aspects

Today's blog focuses on the following issues

Realization idea

In case of network error, this method is feasible to obtain the network status for judgment, but have you thought about it? If you have to make such judgment every time, isn't it a lot of code? Some people will say, wouldn't it be good to encapsulate the code into a class? This can really reduce the amount of code, but it's troublesome to take the initiative to get it every time. In fact, Google has already encapsulated it for us. It will broadcast when the network state changes. We just need to monitor the broadcast.

Is it better to use static broadcasting or dynamic registration broadcasting?

If you don't know, I can only say that the foundation is not solid. The reason why our application wants to monitor the changes of network status is mainly to facilitate processing in case of errors. Of course, we don't need to monitor after exiting our current application, so we choose to dynamically register for broadcasting.

Common ground: both need to be registered in the Android mainest manifest file

Two methods of monitoring network status through broadcast

Static registration

Dynamic registration

Step 1: register in the androidmainest file

Step 2: call context registerreceiver (receiver, intentfilter) to register

Our custom networkchangelistener broadcast

Finally, don't forget to add permissions

Train of thought analysis

From the above code, we can know that we save our current network state in our app, so that when the network state changes, we will automatically change the corresponding state quantity in the app. When we conduct network processing, we only need to obtain the state quantity in the app to judge which network error it belongs to. Is it convenient.

There are three main types of broadcast actions:

WifiManager.WIFI_ STATE_ CHANGED_ ACTION

The opening and closing of this monitoring WiFi has nothing to do with the WiFi connection

WifiManager.NETWORK_ STATE_ CHANGED_ ACTION:

This monitors the WiFi connection status, that is, whether an effective wireless route is connected. When the status of the upper broadcast is WiFi manager.wifi_ STATE_ Disabling, and WiFi_ STATE_ When disabled, you won't receive this broadcast at all.

On the upper broadcast, the received broadcast is WiFi manager.wifi_ STATE_ The enabled status will also receive this broadcast. Of course, the WiFi has not been connected to an effective wireless device just after it is turned on

ConnectivityManager.CONNECTIVITY_ ACTION

This monitors network connection settings, including WiFi and mobile data on and off

It's best to use this monitor. If WiFi is turned on, off, and the available connections on the connection will be monitored. The biggest disadvantage of this broadcast is that it is slower than the above two broadcasts. If you just want to monitor WiFi, I think it is better to use the above two together.

As for the connectivity manager, what is networkinfo? Don't worry. I'll briefly introduce it below.

Connectivity manager and networkinfo

What is connectivity manager mainly used for

How do I get the connectivitymanager object?

Get networkinfo object

There are mainly the following methods

To sum up, there are two ways to know whether the current mobile network or WiFi network has been connected.

The first method is just outdated at api23

The second method

Handling of network error

As mentioned earlier, network errors in this blog are mainly divided into two categories

Here we mainly deal with errors without network. Now I know that there are two main methods to deal with them.

The first approach

When the app starts, check whether it is currently connected to the network. A dialog box pops up. If not, jump to the setting interface or WiFi setting interface or open the mobile network interface.

The second method

In fact, it is similar to the first method, but in each case of error, it will judge whether there is a network at present, and a dialog box does not pop up. Jump to the setting interface or WiFi setting interface or open the mobile network interface. Let's see how to pop up a dialog box and jump to the corresponding setting interface

Here we take the first approach, and the effect diagram is as follows

The code is as follows

Here are some important actions

ACTION_ DATA_ ROAMING_ Settings: jump to the mobile network settings interface

ACTION_ WIFI_ SETTINGS

Activity Action: Show settings to allow configuration of Wi-Fi.

ACTION_ WIRELESS_ SETTINGS

Activity Action: Show settings to allow configuration of wireless controls such as Wi-Fi,Bluetooth and Mobile networks.

For more activity actions, please refer to the official website address

It should be noted that

If the context we use is not the context of activity but the context of application, we need to do the following processing, otherwise an error will be reported

Add the following permissions in androidmainfest.

Intelligent no graph

Intelligent no map. This browsing mode is very common. For example, this mode is seen in UC browser and Netease News. The essence of this mode is to monitor the network status, and then determine whether to load network pictures according to whether it is WiFi.

The renderings are as follows

We can see that when the smart map is turned on, we will not load network pictures if we are not connected to WiFi.

The core code of the implementation is as follows

1) When the smart no graph mode changes, we will save the flag in share preferences

At the same time, in order to keep the interface consistent with the value of isilligentnopic in sharepreferences, we need to call the code

2) In NewsList adapter

As for app. Getinstance(). Iswifi(), it indicates whether WiFi is connected or not. It is realized by listening to the broadcast. It has been explained earlier and will not be described here.

Development tools

Source code download address: https://github.com/gdutxiaoxu/FunAPP.git

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>