简体   繁体   中英

How can I send a request with the mobile data interface when on wifi?

I'm developing an app that has to make all network requests via mobile data, even when it's connected to wifi. The problem is that Android turns off mobile data when connected to wifi for obvious reasons.

I tried to list all the network interfaces so I could use the right one but, when on wifi, the rmnet0 interface even disappears from the list (on a Samsung S8, at least. I tried with a OnePlus and it didn't disappear, but it's not up).

Is there a way to do that, either with Kotlin or using the NDK (C/C++)?

One thing you could do is disable wifi. That can be done programmatically in Android , provided you have the android.permission.CHANGE_WIFI_STATE permission.

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
wifiManager.setWifiEnabled(isWifiEnabled);

With wifi disabled the phone should switch back to mobile data. As already stated in the comments, that could lead to extra costs for the users and you may want to try to improve the response for wifi instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM