Android 仅对服务器请求使用蜂窝(3G、4G、EDGE)数据
Android use only cellular(3G, 4G, EDGE) data for server requests
我有一个应用程序,我希望它只使用蜂窝数据来处理服务器请求。
我不需要禁用 WI-FI完全,例如:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
也许有一种方法可以只为我的应用程序。我注意到我正在使用 okHttp 客户端进行改造,比如:
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();
}
检查应用是否连接到wifi,然后防止请求发生如下:
public static boolean isOnWifi(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return mWifi.isConnected();
}
就像@Edson Menegatti 说的那样,我们可以像他的回答一样实现 smth,但是如果有很多工具,我们为什么要让我们的生活复杂化。所以,我使用了一种 firewall 可以禁用某个应用程序的蜂窝数据。我不得不提一下,我做出这个决定只是因为 我的应用程序是公司内部的,所以我可以管理所有员工的设备。
当然,对于每个请求,我都会检查互联网连接和可用性:
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;
}
不得不提一下 ping 选项,对于新程序员:
-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.
并像这样使用此检查器:
//check connection
if (isNetworkAvailable(context)) {
doServerRequest();
} else {
doConnectionAdvertisment(context);
}
对于那些忘记如何构建弹出对话框的人:
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();
}
我有一个应用程序,我希望它只使用蜂窝数据来处理服务器请求。 我不需要禁用 WI-FI完全,例如:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
也许有一种方法可以只为我的应用程序。我注意到我正在使用 okHttp 客户端进行改造,比如:
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();
}
检查应用是否连接到wifi,然后防止请求发生如下:
public static boolean isOnWifi(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return mWifi.isConnected();
}
就像@Edson Menegatti 说的那样,我们可以像他的回答一样实现 smth,但是如果有很多工具,我们为什么要让我们的生活复杂化。所以,我使用了一种 firewall 可以禁用某个应用程序的蜂窝数据。我不得不提一下,我做出这个决定只是因为 我的应用程序是公司内部的,所以我可以管理所有员工的设备。
当然,对于每个请求,我都会检查互联网连接和可用性:
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;
}
不得不提一下 ping 选项,对于新程序员:
-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.
并像这样使用此检查器:
//check connection
if (isNetworkAvailable(context)) {
doServerRequest();
} else {
doConnectionAdvertisment(context);
}
对于那些忘记如何构建弹出对话框的人:
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();
}