Repository navigation
NetStatusBus 1.0.0
First release published through JitPack:
implementation 'com.github.sunyatas:NetStatusBus:1.0.0'.
Added
NetStatusBus.getNetTypeLiveData(): observe the network type asLiveData<NetType>without
annotations or reflection (pure Java; Kotlin users can callasFlow()).register(subscriber, LifecycleOwner): registers a subscriber and unregisters it automatically
when the owner reachesDESTROYED.NetType.ETHERNETfor wired connections, andNetworkUtils.isVpnActive(). A VPN is reported as
the type of its underlying network (#13).NetworkUtils.getMobileGeneration()returns 2G/3G/4G/5G. It is opt-in: the host app must hold
READ_PHONE_STATE, otherwise it returnsUNKNOWN(#12).- Consumer R8/ProGuard rules that keep
@NetSubscribemethods, so minified apps work without extra
configuration. - JitPack publishing (
jitpack.yml,maven-publishrelease publication with sources jar). - GitHub Actions CI: debug build, unit tests, a minified sample release build that checks the
consumer rules, and apublishToMavenLocalcheck that mirrors the JitPack build. - Unit tests for dispatching, mode filtering, lifecycle binding, weak references, method validation,
subscriber exceptions and the default-network tracker. - Apache-2.0 license, English README with a Chinese translation (
README.zh-CN.md), and this changelog.
Changed
- Migrated to AndroidX;
compileSdk35, Android Gradle Plugin 8, JDK 17 to build.minSdkstays 21. - Network type is derived from
NetworkCapabilitiesinstead of the deprecatedNetworkInfo. - On Android 7.0+ the library follows the system default network via
registerDefaultNetworkCallback, so a switch (for example Wi-Fi to mobile) is reported
immediately and a lateonLostfor the old network is ignored (#4, #11). Mode.WIFI/Mode.MOBILEsubscribers are only called when that network itself connects or
disconnects, not when an unrelated network drops (#7, #9).- Subscriber methods are always invoked on the main thread, and the subscriber registry is
thread-safe (#5). - Subscribers are held through weak references, so a forgotten
unregisterno longer leaks an
Activity or Fragment. @NetSubscribemethods declared in superclasses are discovered, and the lookup is cached per class.NetworkUtils.isNetworkAvailable()no longer requires the network to beVALIDATEDon API 23+.NetworkUtils.openSetting()honors itsrequestCodeand also works with a non-ActivityContext.- Exceptions thrown by a subscriber are logged with
Log.e(tagNetStatusBus) with the real cause
andClass#method; other subscribers are still notified.
Fixed
- After turning Wi-Fi off the library could still report
WIFI(#6). - No network callback was registered on API 21, and calling
init()twice registered it twice. - The library manifest now declares
ACCESS_NETWORK_STATEitself.
Behavior notes when upgrading from the 2019 Bintray/JCenter builds
- The Maven coordinates changed from the defunct Bintray artifact to JitPack
(com.github.sunyatas:NetStatusBus). register()now throwsIllegalArgumentExceptionwhen a@NetSubscribemethod's parameter is
notNetType(or a supertype of it), including methods inherited from a superclass. Before, such
a method was accepted and failed only when an event was dispatched. Methods with more than one
parameter or a non-void return type were already rejected.- Callbacks always arrive on the main thread.
Full changelog: https://github.com/sunyatas/NetStatusBus/blob/v1.0.0/CHANGELOG.md