Android force app to use mobile data channel

前端 未结 1 1731
难免孤独
难免孤独 2021-01-03 08:56

I am doing some investigation on an app I am developing. The issue is the app requires connection to mobile data network so when wifi is on, it will not route properly sinc

相关标签:
1条回答
  • 2021-01-03 09:33

    You can't explicitly force the communications channel on a per-app basis (you can request to use a preferred mode via ConnectivityManager.setNetworkPreference(...), but that's not "forcing").

    Though it's probably terrible UX, you can inform the user that your app disallows use of WiFi, then disable their WiFi if they want to continue. To do so, you need the ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions. The code would look something like this:

    manager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
    
    if(manager.isWifiEnabled()) {
        manager.setWifiEnabled(false);
    }
    // and to be sure:
    ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
    
    0 讨论(0)
提交回复
热议问题