Android only uses cellular (3G, 4G, EDGE) data for server requests

I have an application and I would like it to only use cellular data for server requests. I don't need to turn off WI-FI completely , for example:

  WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  wifiManager.setWifiEnabled(false);

      

There might be a way to do it just for my application . I note that I am using modification with okHttp client, smth like:

    private static void setupRestClient() {

        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(5, TimeUnit.SECONDS);
        client.setReadTimeout(5, TimeUnit.SECONDS);
        client.setRetryOnConnectionFailure(true);

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(new EndpointManager())
                .setClient(new OkClient(client))
                .build();

    }

      

+3


source to share


2 answers


as @Edson Menegatti said, we can implement smth like his answer, but why should we complicate our life if there are many tools for that. So, I used a kind of firewall that can disable cellular data for a specific application. I should mention that I only made this decision because my application is an internal company , so I can manage all employees.

Of course, I have a check for internet connectivity and availability for each request:

 public boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnectedOrConnecting() && isOnline();
}

public boolean isOnline() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("/system/bin/ping -c1 -w1 8.8.8.8");
        int exitValue = ipProcess.waitFor();
        return (exitValue == 0);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return false;
}

      

The ping option for new encoders should be mentioned:

    -c count

                  Stop  after  sending  count  ECHO_REQUEST packets. With deadline option, ping
                  waits for count ECHO_REPLY packets, until the timeout expires.


   -w deadline
                      Specify a timeout, in seconds, before ping exits regardless of how many pack‐
                      ets  have  been sent or received. In this case ping does not stop after count
                      packet are sent, it waits either for deadline expire or  until  count  probes
                      are answered or for some error notification from network.

      



And use this check:

//check connection
        if (isNetworkAvailable(context)) {
            doServerRequest();                
        } else {
            doConnectionAdvertisment(context);
        }

      

For those who forgot how to create a popup dialog:

public void doCheckConnectionAdvertisment(final Context context) {
        //alert dialog message
        new AlertDialog.Builder(context)
                .setTitle("Caution!")
                .setMessage("Please check your internet connection and try again")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //do something
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setCancelable(false)
                .show();
    }

      

0


source


Make sure the app is connected to Wi-Fi and then prevents the following requests from being executed:



public static boolean isOnWifi(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return mWifi.isConnected();
}

      

+3


source







All Articles