I want the android application to use only mobile data even if WIFI and mobile data both on in Android 6. How can I bind the android process to Mobile data only.
Please suggest. Examples will be good.
            Asked
            
        
        
            Active
            
        
            Viewed 3,943 times
        
    2
            
            
        
        Vadim Kotov
        
- 8,084
 - 8
 - 48
 - 62
 
        Pallav Singh
        
- 103
 - 3
 - 10
 
3 Answers
5
            
            
        You can do this pretty easy if you're android version is M or above. First, you need the right manifest permissions: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
Here is an example method that should do what you need:
public static void forceConnectionToMobile2(Context context) {
    final ConnectivityManager connection_manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        NetworkRequest.Builder request = new NetworkRequest.Builder();
        Log.d(TAG,"request TRANSPORT_CELLULAR");
        request.addCapability(NetworkCapabilities.TRANSPORT_CELLULAR);
        connection_manager.requestNetwork(request.build(), new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    Log.d(TAG,"binding app to cellular network");
                    connection_manager.bindProcessToNetwork(network);
                }
            }
        });
    }
}
        davidmc.w3sys
        
- 51
 - 3
 
- 
                    I tried this with wifi having no internet and turning on cellular data, but i am not able to use internet then... – Sahil Shokeen Jul 10 '20 at 11:43
 
2
            
            
        boolean mobileDataEnabled = false;
        try {
            Class cmClass = Class.forName(connection_manager.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(connection_manager);
        } catch (Exception e) {
        }
First check if mobile data enable or disable then do 
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            NetworkRequest.Builder request = new NetworkRequest.Builder();
            request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
            request.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
            connection_manager.requestNetwork(request.build(), new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
connection_manager.bindProcessToNetwork(network);
}
}
        Ashish Garg
        
- 162
 - 1
 - 11
 
0
            
            
        You can visit Android force app to use mobile data channel
    manager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
if(manager.isWifiEnabled()) {
    manager.setWifiEnabled(false);
}
// and to be sure:
ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
- 
                    I dont want to _disable WIFI in the device level_. just want to use **mobile data only** for my app. – Pallav Singh Oct 24 '16 at 11:30
 - 
                    boolean mobileDataEnabled = false; try { Class cmClass = Class.forName(connection_manager.getClass().getName()); Method method = cmClass.getDeclaredMethod("getMobileDataEnabled"); method.setAccessible(true); mobileDataEnabled = (Boolean) method.invoke(connection_manager); } catch (Exception e) { } – Ashish Garg Sep 29 '17 at 10:12