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
Information flow advertising style - live broadca...
The Tick smartwatch, which has completely state-ow...
The core essence of new media is actually tools. ...
Most mature products have their own points system...
The iPhone 13 series released this year finally c...
China Science and Technology News Network, Februa...
User growth is no longer a new concept. Many comp...
Harvest in China | Satellite view of autumn harve...
Staying young forever is everyone's dream, bu...
New Media New media is a relative concept. It is ...
In the era of mobile Internet, online life is bec...
On January 23, the Central Meteorological Observa...
Recently, a video of a Tibetan brown bear chargin...
In the past few months, we have participated in t...