Skip to content

NetworkObserver--极简可实时监控网络状态,兼容至Android 10.0

Notifications You must be signed in to change notification settings

stevenduan17/NetworkObserver

Repository files navigation

NetworkObserver--A simple and convenient library to monitor network status in real time ,compatible with Android 10.0.

Download API Licenses

In many cases, network status needs to be monitored in our application,we want to know current network status: WLAN,mobile network or event 2G,3G,4G,5G. NetworkObserver uses the idea of EventBus to observe network status changes everywhere.

中文文档

Gradle

dependencies {
    implementation 'com.steven:networkobserver:$latest_version'
}

AndroidX

If your application build with androidX,please use version 0.2.0 and above.Use support library can continue to use 0.1.1 version. If the target version is lower than 29 (Android Q), the 5G is a new field added to Android Q to determine the mobile network status and the related methods may fail, but the basic network type judgment is not affected.

Usage

  1. add permissions to manifest
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  1. subscribe & register
NetworkObserver.getDefault().apply {
    subscribe(this@MainActivity)
    register(this@MainActivity)
}
  1. observe on Activity or Fragment
 @OnNetworkChange
 fun onNetworkChange(type: NetworkType) {
     when (type) {
         NetworkType.WIFI -> {
             textView.text = "WLAN"
         }
         NetworkType.MOBILE -> {
             textView.text = "MOBILE NETWORK"
         }
         NetworkType.NONE -> {
             textView.text = "NONE"
         }
     }
 }
  1. unsubscribe & unregister
 NetworkObserver.getDefault().apply {
    unsubscribe(this@MainActivity)
    unregister(this@MainActivity)
 }
  1. get subtypes of mobile network Global util method #getMobileNetworkSubType(context) can judge 2G, 3G, 4G, but 5G requires AndroidX, and target version is 29 (Android Q).

Notes

  • Register and unregister must occur in pairs, such as register in Activity's onCreate() and unregister in onDestroy().
  • The parameters passed in (context, observer), observer is the subject to be monitored and it can be Activity or Fragment.
  • If you want to use it globally, please subscribe and unsubscribe in Application. You only need to register/unregister on the page, no subscription is required.

Licence

Copyright (c) 2020 stevenduan17

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

NetworkObserver--极简可实时监控网络状态,兼容至Android 10.0

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages