A new project has just started its promotion work, and the marketing staff complained to me that there are always various problems when users use it. Most of the problems are caused by improper user operation, but in the eyes of users, the conclusion is "your APP is not easy to use." For example, some users disable the APP from accessing the mobile network, or some users simply do not turn on the mobile data switch or the WIFI switch. However, as developers, we should avoid users having to think about it. When users have problems using the APP, the APP should be able to guide users to the settings, so there is this article. We hope to remind users of the current network status in time when the user's network connection is unavailable. When the connection is restored, hide the prompt view, and we hope that this prompt view can work on all pages that require the network. The idea is as follows: use BaseActivity, all pages inherit this file, and in this file, display and hide prompts according to the network status. Okay, enough of the crap, show you the code. 1. Implement a broadcast receiver that monitors network status changes We use a broadcast receiver to receive the Intent of network changes. Here we use the static registration method directly, because we don't need to register this Receiver separately on each page, which is too heavyweight. NetworkConnectChangedReceiver.java
Event
Determine whether the network connection is available:
Static registration of Receiver:
2. Listen to events and process prompt views in BaseActivity When you see EventBus, do you already know how I implemented it? (laughing XD) Yes, it is the EventBus that no one has mentioned for a long time. Of course, you can also use the observer mode to implement it, so you don’t have to rely on third-party libraries, but what we need is to implement it quickly and change the original code as little as possible. Introducing the observer mode is obviously not as convenient as directly using EventBus. BaseActivity.java
By default, all pages that inherit BaseActivity will display a prompt when the network status changes or there is no network. If a page does not need a network status prompt, you can call setCheckNetWork(false) in the onCreate method of the page. Since all my pages have a 50dp height toolbar, I set the top margin directly in the R.layout.layout_network_tip file. You can also set the position of the prompt dynamically for each page by setting mLayoutParams.x = 0; mLayoutParams.y = 0; in BaseActivity. The final effect is as follows: ToDo All pages should be able to automatically re-initiate network requests after the network connection is restored. The implementation principle is actually very simple. Add a reConnect() method in BaseActivity and call it when the network is restored to remove the prompt View. Rewrite this method in each page. |
>>: Mobile payment ranking: WeChat first, Alipay second, Apple Pay fourth
What is the difference between purified water and...
On September 17, Chery Automobile Co., Ltd. (here...
Principal Jiaqi's "Douyin Fans Exclusive...
Now that the weather is getting colder, there are...
For entrepreneurs, although mini program developm...
When it comes to fried chicken fast food brands, ...
When was the first time you learned about short v...
In recent years, the wedding market has shown a m...
After a long wait, the second shoe finally droppe...
If 2016 is the year when Type-C was fully populari...
Is it fog or haze? Environmental Science Autumn a...
Great operations all have their own set of method...
According to relevant national plans, the product...
On March 28, Microsoft officially launched Office ...
The author of this article will explain the mista...