Android monitors (monitors) changes in network connection status in real time

After reading a lot of online, they are not what they want. So I wrote one for reference.

There are many ways to achieve this. I think that if there are any shortcomings and bugs, I hope you can point out and make common progress....

Let's briefly talk about the idea: the system will broadcast when the network changes. So let's listen to the broadcast and use the interface callback to notify the activity to do the corresponding operation..

Steps:

1. Write a tool class to judge the network

2. First write a class to inherit broadcastreceiver. (don't forget to register in the manifest file) (thanks for itzxl's reminder) you need to add permissions in the manifest file < uses permission Android: name = "Android. Permission. Access_network_state" > < / uses Permission >

3. Write a callback interface

4. Baseactivity implements this interface

Upper Code:

Remember to register in manifest

It should be explained that if the mobile phone is not used for a long time with WiFi on, the automatic black screen will turn off the traffic for a long time, so the promotion language of the monitoring status is hidden when it is pulled down and refreshed!

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
分享
二维码
< <上一篇
下一篇>>