Binding to cellular network with bindProcessToNetwork does not work

早过忘川 提交于 2020-04-07 07:08:26

问题


I am trying to bind to a cellular network on demand (wifi is still running). I have the following code:

            override fun onAvailable(network: Network) {
                super.onAvailable(network)
                Toast.makeText(this@MainActivity, "CELLULAR READY", Toast.LENGTH_SHORT).show()
                Log.e("darran", "network info is: " +connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE))
                Log.e("darran", "network is metered: " +connectivityManager.isActiveNetworkMetered)
                Log.e("darran", "bind is tru: " +connectivityManager.bindProcessToNetwork(network))
                Log.e("darran", "bound network is: " +connectivityManager.boundNetworkForProcess)
                GlobalScope.launch {
                    fetchIp(network)
                }
            }

When I try to bind to the cellular network while wifi is up boundNetworkForProcess is not the cellular network, but still the wifi network.

If I disable the wifi network this will work every time.

I also have no problem binding immediately between ethernet and wifi. It appears mobile data can not be bound to on demand even if the network is available. How do I immediately bind to the cellular network regardless of the state of the other networks?

The full project is here:

https://github.com/dazza5000/network-switch-poc/blob/master/app/src/main/java/com/fivestars/networkswitchpoc/MainActivity.kt


回答1:


Android exposes a property through developer options that allows you keep mobile data always active. Enabling this flag allows you to switch between wifi and cellular data connections and make successful network requests on the requested network immediately.

This flag is also defined as a global setting.

Settings.Global.MOBILE_DATA_ALWAYS_ON

https://github.com/aosp-mirror/platform_frameworks_base/blob/e80b45506501815061b079dcb10bf87443bd385d/services/core/java/com/android/server/ConnectivityService.java#L951



来源:https://stackoverflow.com/questions/60663550/binding-to-cellular-network-with-bindprocesstonetwork-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!