0

Just like in Google Play Store you have an option to only update the apps over Wifi and not over Mobile Data connection i.e " Auto-update apps over Wi-Fi only." or an option to Update apps through both Mobile and Wifi

I am developing an applications that will download some media files from a server but i want to enable an option that the user can toggle if they want to only download over wifi or download through both Any help/suggestion will be welcome

Shayan
  • 1

2 Answers2

0

You should look at this guide on network usage management.

You can use NetworkInfo to achieve what you're looking for:

NetworkInfo: Describes the status of a network interface of a given type (currently either Mobile or Wi-Fi).

private static final String DEBUG_TAG = "NetworkStatusExample";
...
ConnectivityManager connMgr = (ConnectivityManager)
        getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);

If the user chose to download only over Wi-Fi, you can reject the operation when the network used is not Wi-Fi.

Batman
  • 282
  • 3
  • 12
0

You can try JobScheduler and define the network type from JobInfo

Simple guide here

    JobInfo job = new JobInfo.Builder(JOB_ID, new ComponentName(this, UpdateJobService.class))  
   .setRequiredNetworkType(JobInfo.TRANSPORT_WIFI)
   .setRequiresCharging(true)
   .build();
h0102
  • 236
  • 3
  • 11