Repository navigation
Releases
NetStatusBus 1.1.0
Added
-
NetStatusBus.addListener(OnNetTypeChangeListener)/removeListener(...): an annotation-free, reflection-free callback for Java and Kotlin. It receives the current type right away and then every change on the main thread, including while the UI is in the background (unlikeLiveData).addListener(listener, LifecycleOwner)removes it automatically onDESTROYED. A listener that throws is logged and does not affect other listeners or subscribers. -
NetworkUtils.getNetType(Context),isNetworkAvailable(Context),isVpnActive(Context)andgetMobileGeneration(Context): the same checks without callinginitfirst (for example in aBroadcastReceiver, a WorkManager worker, or another library). Only the application context is kept. -
NetStatusBus.getCurrentNetType(): the type the bus last delivered, read from a cached field without querying the system (any thread).NetStatusBus.isInitialized()tells whetherinithas been called.
Changed
- Using the bus or a no-argument
NetworkUtilsmethod beforeinitnow throwsIllegalStateExceptionwith a message that explains the fix, instead of a bareRuntimeException("application is empty");registerandaddListenercalled beforeinitnow throw the sameIllegalStateExceptioninstead ofIllegalArgumentException.IllegalStateExceptionis aRuntimeException, so existingcatchblocks still work. unregisterAllObserver()also removes listeners added withaddListener.
Fixed
- Registering the same subscriber from several threads at the same time could add it twice, so every network change was delivered to it twice. Registration and unregistration now share a lock; dispatch stays lock-free.
- Calling
registeroraddListeneroff the main thread while the network was changing could deliver the "current type" callback after a newer change had already been delivered, so the subscriber or listener ended up on an outdated type until the next change (getNetTypeLiveData()was not affected). The initial callback is now skipped when a newer change has already been delivered.
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