Detailed explanation of switching monitoring between WiFi and data traffic in Android

Recently, I have been working on a WiFi and mobile data monitoring function, which has been tossing around for a while. The main function of this module is to monitor the switching between WiFi and data traffic of the whole app, so that users can use dedicated traffic instead of WiFi. Give a pop-up window, click OK, automatically switch data traffic and turn off WiFi. My idea is to write a static broadcast, monitor in the broadcast, enable the system pop-up window, click OK, and automatically switch the network. There is a pit in this, that is, the pop-up window will be called many times in the broadcast. In fact, it has only been called once, but in fact, the pop-up windows of the system will be superimposed one after another for many times. After a long time, it has finally been done well, It was originally caused by the system broadcast. See the code for details:

Network encapsulation connectivitymanager:

Connectivitymanager is a network connection management class, which encapsulates the details of network requests, including the on / off status of WiFi and mobile data traffic, on / off status, connection status, etc. it is very suitable for network monitoring. There is also a class WiFi manager, which is specially used for WiFi monitoring. Its monitoring effect is more detailed and rich than that of connectivity manager. It can be detected, but it can't take effect on traffic. It is sufficient to use the connectivity manager here.

1、 Register broadcast

1. Write a class that inherits from broadcastreceiver.

2、 Pop up window

Pop ups generally use four common methods:

1. Using popupwindow

2. Using dialog

3、WindowManager

4. System pop-up window

The general pop-up window needs to be attached to the activity. The pop-up window in the activity cannot be simply used in the service. The pop-up window of the system is generally used. It has a high priority, covers the top layer of the application interface, and setType (WindowManager. Layoutparams. Type_system_alert) should be set, otherwise it will crash.

3、 Network switching

WiFi network switching is relatively easy. You can complete the switching by directly setting setwifi enabled, but the switching of data traffic is troublesome. His method is private and cannot be called. We can find his method to call through reflection: see the code for details

2. Register broadcast in manifest file

Add permissions:

The above code describes the real-time detection of network handover in detail. More detailed and rich are not written here.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of 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
分享
二维码
< <上一篇
下一篇>>