简体   繁体   English

如何启用/禁用手机数据下载

[英]How to enable/disable downloading on mobile data

Just like in Google Play Store you have an option to only update the apps over Wifi and not over Mobile Data connection ie " Auto-update apps over Wi-Fi only." 就像在Google Play商店中一样,您可以选择仅通过Wifi而不是通过移动数据连接更新应用,即“仅通过Wi-Fi自动更新应用”。 or an option to Update apps through both Mobile and Wifi 或通过移动和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 我正在开发一个将从服务器下载某些媒体文件的应用程序,但是我想启用一个选项,使用户可以切换是否只想通过wifi下载或通过两者下载的任何帮助/建议。

You should look at this guide on network usage management. 您应该阅读有关网络使用管理的指南

You can use NetworkInfo to achieve what you're looking for: 您可以使用NetworkInfo实现所需的功能:

NetworkInfo: Describes the status of a network interface of a given type (currently either Mobile or Wi-Fi). NetworkInfo:描述给定类型(当前为移动或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. 如果用户选择仅通过Wi-Fi下载,则当使用的网络不是Wi-Fi时,您可以拒绝该操作。

You can try JobScheduler and define the network type from JobInfo 您可以尝试JobScheduler并从JobInfo定义网络类型

Simple guide here 这里的简单指南

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM