In 10 minutes, you can realize global prompt of network status changes in APP

In 10 minutes, you can realize global prompt of network status changes in APP
  • Never expect users to follow your pre-set steps to operate the app

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

  1. public class NetworkConnectChangedReceiver extends BroadcastReceiver {
  2. private static final String TAG = "NetworkConnectChanged" ;
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. //**Judge whether the current network connection status is available*/
  6. boolean isConnected = NetUtils.isConnected(context);
  7. Log.d(TAG, "onReceive: current network " + isConnected);
  8. EventBus.getDefault().post(new NetworkChangeEvent(isConnected));
  9. }
  10. }

Event

  1. public class NetworkChangeEvent {
  2. public boolean isConnected; //Is there a network?
  3.  
  4. public NetworkChangeEvent(boolean isConnected) {
  5. this.isConnected = isConnected;
  6. }
  7. }

Determine whether the network connection is available:

  1. /**
  2. * Determine whether the network is connected
  3. * @param context
  4. * @return  
  5. */
  6. public   static boolean isConnected(Context context) {
  7. ConnectivityManager connectivity = (ConnectivityManager) context
  8. .getSystemService(Context.CONNECTIVITY_SERVICE);
  9.  
  10. if ( null != connectivity) {
  11. NetworkInfo info = connectivity.getActiveNetworkInfo();
  12. if ( null != info && info.isConnected()) {
  13. if (info.getState() == NetworkInfo.State.CONNECTED) {
  14. return   true ;
  15. }
  16. }
  17. }
  18. return   false ;
  19. }

Static registration of Receiver:

  1. <receiver android: name = ".receiver.NetworkConnectChangedReceiver" >
  2. <intent-filter>
  3. < action android: name = "android.NET.conn.CONNECTIVITY_CHANGE" />
  4. < action android: name = "android.Net.wifi.WIFI_STATE_CHANGED" />
  5. < action android: name = "android.net.wifi.STATE_CHANGE" />
  6. </intent-filter>
  7. </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

  1. public class BaseActivity extends Activity {
  2.  
  3. protected Context mContext;
  4. protected ACache mACache;
  5. protected boolean mCheckNetWork = true ; //Default check network status
  6. View mTipView;
  7. WindowManager mWindowManager;
  8. WindowManager.LayoutParams mLayoutParams;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. mContext = this;
  14. this.mACache = ACache.get(mContext);
  15. MyApp.addActivity(this);
  16. initTipView(); //Initialize the prompt View  
  17. EventBus.getDefault().register(this);
  18. }
  19.  
  20. @Override
  21. protected void onResume() {
  22. super.onResume();
  23. MobclickAgent.onResume(this);
  24. //When opening the app without a network, the system will not send an Intent indicating a network status change, so you need to check it manually.
  25. hasNetWork(NetUtils.isConnected(mContext));
  26. }
  27.  
  28. @Override
  29. protected void onPause() {
  30. super.onPause();
  31. MobclickAgent.onPause(this);
  32. }
  33.  
  34. @Override
  35. protected void onDestroy() {
  36. super.onDestroy();
  37. MyApp.removeActivity(this);
  38. EventBus.getDefault().unregister(this);
  39. }
  40.  
  41. @Override
  42. public void finish() {
  43. super.finish();
  44. // When the prompt View is dynamically added, closing the page directly will cause the View to overflow memory, so it needs to be removed at finish time
  45. if (mTipView != null && mTipView.getParent() != null ) {
  46. mWindowManager.removeView(mTipView);
  47. }
  48. }
  49.  
  50. @Subscribe(threadMode = ThreadMode.MAIN)
  51. public void onNetworkChangeEvent(NetworkChangeEvent event) {
  52. hasNetWork(event.isConnected);
  53. }
  54.  
  55. private void hasNetWork(boolean has) {
  56. if (isCheckNetWork()) {
  57. if (has) {
  58. if (mTipView != null && mTipView.getParent() != null ) {
  59. mWindowManager.removeView(mTipView);
  60. }
  61. } else {
  62. if (mTipView.getParent() == null ) {
  63. mWindowManager.addView(mTipView, mLayoutParams);
  64. }
  65. }
  66. }
  67. }
  68.  
  69. public void setCheckNetWork(boolean checkNetWork) {
  70. mCheckNetWork = checkNetWork;
  71. }
  72.  
  73. public boolean isCheckNetWork() {
  74. return mCheckNetWork;
  75. }
  76.  
  77. private void initTipView() {
  78. LayoutInflater inflater = getLayoutInflater();
  79. mTipView = inflater.inflate(R.layout.layout_network_tip, null ); //Prompt View layout
  80. mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
  81. mLayoutParams = new WindowManager.LayoutParams(
  82. ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,
  83. WindowManager.LayoutParams.TYPE_APPLICATION,
  84. WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
  85. | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
  86. PixelFormat.TRANSLUCENT);
  87. //When using non-CENTER, you can change the position of View by setting the XY value
  88. mLayoutParams.gravity = Gravity.TOP ;
  89. mLayoutParams.x = 0;
  90. mLayoutParams.y = 0;
  91. }
  92. }

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.

<<:  The world's largest third-party Android system LineageOS announced that it would stop maintaining 30 models

>>:  Mobile payment ranking: WeChat first, Alipay second, Apple Pay fourth

Recommend

Principal Jiaqi's "Exclusive for Douyin Fans - Heart Hunting Plan"

Principal Jiaqi's "Douyin Fans Exclusive...

How much does it cost to be a Heyuan agent for an automatic food ordering app?

For entrepreneurs, although mini program developm...

KFC’s private domain growth strategy

When it comes to fried chicken fast food brands, ...

Group buying ends: How does Hongtu Sanbao “blend” Lashou?

After a long wait, the second shoe finally droppe...

Have you not yet distinguished between "fog" and "haze"?

Is it fog or haze? Environmental Science Autumn a...

How to become a great operator? I'll give you 2 methods

Great operations all have their own set of method...

Office for iPad

On March 28, Microsoft officially launched Office ...

Why do most startup marketing fail?

The author of this article will explain the mista...