preface

Android development often encounter the need to monitor network changes in the scene, such as play, such as access to the network for interruption prompts, so there is today’s AndroidNetworkWatcher.

The project address

AndroidNetworkWatcher:github.com/xiong-it/An…

The technology principle

It mainly encapsulates the network change monitoring broadcast, and defines runtime annotations to call the business layer code when the network changes and notify the network changes.

usage

Gradle remote dependencies are not available because they have not been uploaded to JCenter. You can download the source code locally or package it yourself.

  1. Initialize Watcher in Application:
public class App extends Application {
    @Override
    public void onCreate() { super.onCreate(); NetworkStateWatcher.getDefault().init(this); }}Copy the code
  1. Register an observer, using the Activity as an example
@Override
protected void onStart() { super.onStart(); / / registered monitoring network, the incoming Object type parameter NetworkStateWatcher. GetDefault () registerObserver (this); }Copy the code
  1. Unregister an observer, as in the Activity example
@Override
    protected void onStop() { super.onStop(); / / cancellation of monitoring network, the incoming Object type parameter NetworkStateWatcher. GetDefault () unRegisterObserver (this); }Copy the code
  1. Define a function to receive network changes (2 methods)

    4.1 Type 1: Monitor all network changes

    /** * Network changes ** @paramtypeNetwork type */ @NetworkStatechanged (notifyOnAppStart =false)
    void onNetworkStateChanged(int type) {
        switch (type) {
            case NetworkTypeEnum.NETWORK_2G:
            case NetworkTypeEnum.NETWORK_3G:
            caseNetworkTypeEnum.NETWORK_4G: // Switch to the mobile networkbreak;
    
            caseNetworkTypeEnum.NETWORK_WIFI: // Switch to wifibreak;
    
            caseNetworkTypeEnum.NET WORK_NO: / / broken networkbreak; }}Copy the code

    4.2 Second: Listen for specific network changes

    @onNetworkTypechangedto (type = NETWORK_WIFI)
    void onWifiConnected() {
        Toast.makeText(  this, "NetworkStateChanged>>>WIFI", Toast.LENGTH_SHORT).show(); } @onNetworkTypechangedto (type = NetworkTypeEnum.NETWORK_MOBILE)
    void onMobileNetworkConnected() {
        Toast.makeText(this, "NetworkStateChanged>>>Mobile networks", Toast.LENGTH_SHORT).show();
    }
    Copy the code
  2. Stop network listening on the app home screen. When you exit the app, stop network listening in the onDestroy method of the last Activity in the app’s Activity stack.

    @Override
    protected void onDestroy() { super.onDestroy(); / / in the main interface/stack stopped an Activity network monitor NetworkStateWatcher. GetDefault (). A stopWatch (); }Copy the code

conclusion

No fancy technology, observer, annotation, reflection, broadcast receiver. That’s all, but it simplifies the business layer code for you, me, and others. AndroidNetworkWatcher:github.com/xiong-it/An…

// TODO

  1. Upload to jCenter repository for gradle remote dependency
  2. Use APT or Tranform to further simplify business code
  3. Collecting Issues
  4. Welcome to discuss and exchange