AndroidNetworkWatcher: simplify network monitoring service code

Foreword

Android developers often encounter scenarios need to monitor network changes, such as playing, for example, carried off the network prompts for access to the network, so with today's AndroidNetworkWatcher.

project address

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

Technical principles

The main change is to listen to the broadcast network is encapsulated, and annotation defines runtime, call the business layer code when network changes, notify the network changes.

usage

Because not uploaded to jcenter, not by remote gradle dependent, readers can download their own local source dependent, or reference source their own package.

  1. In the Application Initialization Watcher:
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        NetworkStateWatcher.getDefault().init(this);
    }
}
复制代码
  1. Registration observer to Activity example
@Override
protected void onStart() {
    super.onStart();
    // 注册网络监听,传入Object类型参数
    NetworkStateWatcher.getDefault().registerObserver(this);
}
复制代码
  1. Cancellation observer to Activity example
@Override
    protected void onStop() {
        super.onStop();
        // 注销网络监听,传入Object类型参数
        NetworkStateWatcher.getDefault().unRegisterObserver(this);
    }
复制代码
  1. Receiving a definition of a function of changes in the network (2 methods)

    4.1 The first: listen to all network changes

    /**
     * 网络发生变化
     *
     * @param type 网络类型
     */
    @NetworkStateChanged(notifyOnAppStart = false)
    void onNetworkStateChanged(int type) {
        switch (type) {
            case NetworkTypeEnum.NETWORK_2G:
            case NetworkTypeEnum.NETWORK_3G:
            case NetworkTypeEnum.NETWORK_4G:
                // 切换到移动网络
                break;
    
            case NetworkTypeEnum.NETWORK_WIFI:
                // 切换到wifi
                break;
    
            case NetworkTypeEnum.NETWORK_NO:
                // 断网
                break;
        }
    }
    复制代码

    4.2 second: monitor specific network changes

    // 监听网络切换到wifi
    @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();
    }
    复制代码
  2. Stop when you exit the network monitoring app, namely onDestroy app stack method Activity Activity last stop of network monitoring app in the main interface, lib also listen to the broadcast network is written off.

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 在app主界面/栈中最后一个Activity停止网络监听
        NetworkStateWatcher.getDefault().stopWatch();
    }
    复制代码

Epilogue

Useless technology, the viewer, annotations, reflection, radio receivers on what tall. Nothing more, but it can streamline business layer code to facilitate you and me. AndroidNetworkWatcher: github.com/xiong-it/An...

// ALL

  1. Jcenter uploaded to the warehouse, to facilitate remote depend gradle
  2. Apt to further simplify the use of service code or tranform
  3. Collection issue opinions
  4. Welcome to discuss and exchange

Guess you like

Origin juejin.im/post/5da8254e5188252fac2c784f