1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 | @TargetApi(21)
private void forceSendRequestByMobileData() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NET_CAPABILITY_INTERNET);
//强制使用蜂窝数据网络-移动数据
builder.addTransportType(TRANSPORT_CELLULAR);
NetworkRequest build = builder.build();
connectivityManager.requestNetwork(build, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
try {
URL url = new URL("");
HttpURLConnection connection = (HttpURLConnection)network.openConnection(url);
/*******省略参数配置*******/
connection.connect();
/*******数据流处理*******/
} catch (Exception e) {
}
}
});
}
|