Route network requests only over mobile data connection
Is there a way to force all my network requests to be made through my APN (i.e. over a mobile network data connection) and not through wifi, without having to disable wifi?
I want to know if it is possible for my Android app to use exclusively the network access point.
I'm already a开发者_C百科ble to set the default network APN and to use it but for this I have to disable the Wifi first.
Considering the initial login to google to provision android devices doesn't do this, its probably problematic.
Also think about the fact that users might expect the phone to only use wifi data when it is connected. That is what I do anyway. The 3G-icon (with its up/down traffic indicators) also disappears whenever the wifi-icon is visible, so there must be some thought to this, which basically boils down to not using 3g when wifi is available.
This functionality was added in lollipop (with the advent of wifi enabled devices it became necessary). Just get the instance of network that corresponds to your data network from the connectivity manager. Then call network.openConnection(url).
Reference: android developers blog
精彩评论