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
User growth is an eternal topic in the Internet c...
Since last year, as the domestic mobile phone mar...
Since the global outbreak of the COVID-19 pandemi...
Preface In the process of developing APP, enginee...
Conversion is the core of the entire promotion pr...
Preface I recently browsed recruitment informatio...
The overseas Honda HR-V is actually the domestic ...
[[409110]] Scanning a QR code seems convenient, b...
It's Wednesday, does anyone want to go to wor...
Experts in this article: Sun Yan, Chief Physician...
The feathered, sickle-shaped claws Did giant dino...
Many people cannot express their love for music i...
The article is very long and you may not be able ...
On July 17, Didi announced a strategic partnershi...
In recent years, information flow ads have become...