registerDefaultNetworkCallback 和 registerNetworkCallback 的区别
我遇到了 registerDefaultNetworkCallback 和 registerNetworkCallback 同时为 API 28 更新我的 Android 应用程序.
查看文档后,我找不到注册网络回调和注册默认网络回调之间的区别.
什么时候用哪个?
提前致谢:)
解决方案据我了解,registerDefaultNetworkCallback
和 registerNetworkCallback
的区别只是基于自定义.registerDefaultNetworkCallback
(令人惊讶地)作为默认网络侦听器工作,而 registerNetworkCallback
它更具可配置性.例如:
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManagerval builder = NetworkRequest.Builder()builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)val networkRequest = builder.build()连接管理器.registerNetworkCallback(networkRequest,对象:ConnectivityManager.NetworkCallback () {覆盖有趣的可用(网络:网络?){super.onAvailable(网络)Log.i("测试", "网络可用")}覆盖 fun onLost(network: Network?) {super.onLost(网络)Log.i("测试", "连接丢失")}})
这里的 onAvailable
只有当用户连接到蜂窝网络时才会被调用(通过连接到 WiFi 它不会记录任何内容).与蜂窝网络断开连接时,onLost
也是如此.
如果我们这样做:
connectivityManager.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() {覆盖有趣的可用(网络:网络?){super.onAvailable(网络)Log.i("测试", "默认 -> 网络可用")}覆盖 fun onLost(network: Network?) {super.onLost(网络)Log.i("测试", "默认 -> 连接丢失")}})
当用户连接(或断开)网络(可以是 WiFi 或蜂窝网络)时,这两个函数都用作默认回调.
这些只是一些非常基本的例子.当然NetworkRequest
可以通过设置capability
或者transportType
进行很多配置.您可以在 NetworkRequest 的官方文档中阅读更多相关信息.p>
I came across registerDefaultNetworkCallback and registerNetworkCallback while updating my Android app for API 28.
Having reviewed the documentation, I cannot find the difference between registering a network callback and registering a default network callback.
When will one use which?
Thanks in advance :)
解决方案As far as I understood, the difference between registerDefaultNetworkCallback
and registerNetworkCallback
it's only based on customisation.
registerDefaultNetworkCallback
works (surprisingly) as a default network listener, while registerNetworkCallback
it's more configurable. For example:
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val builder = NetworkRequest.Builder()
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
val networkRequest = builder.build()
connectivityManager.registerNetworkCallback(networkRequest,
object : ConnectivityManager.NetworkCallback () {
override fun onAvailable(network: Network?) {
super.onAvailable(network)
Log.i("Test", "Network Available")
}
override fun onLost(network: Network?) {
super.onLost(network)
Log.i("Test", "Connection lost")
}
})
Here onAvailable
will be called only if the user connects to a cellular network (by connecting to WiFi it won't log anything). The same does onLost
when disconnecting from cellular network.
If we do it like this:
connectivityManager.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network?) {
super.onAvailable(network)
Log.i("Test", "Default -> Network Available")
}
override fun onLost(network: Network?) {
super.onLost(network)
Log.i("Test", "Default -> Connection lost")
}
})
Both functions work as default callbacks when the user is connecting (or disconnecting) to/from a network (it can be either WiFi or cellular).
These are just some very basic examples. Of course NetworkRequest
can have a lot of configurations by setting its capability
or transportType
. You can read more about these in the official documentation of NetworkRequest.
相关文章