我必须在Android上同时使用WiFi和lte。
使用ConnectivityManager,我可以在wifi激活时通过lte发送请求
然而,我从未同时使用过这两者。
我目前使用这些方法。
private static ConnectivityManager cm;
private static NetworkRequest.Builder req;
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
req = new NetworkRequest.Builder();
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
//here you can use bindProcessToNetwork
cm.bindProcessToNetwork(network);
}
});
有没有办法可以同时申请wifi和lte?
答案 0 :(得分:0)
您可以使用NetworkCallback的onAvailable()来检查网络更改。 此外,bindProcessToNetwork()声明只使用某些网络。 使用ConnectivityManager.getAllNetworks()和我下面的简单示例。
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
Network []networks = connectivityManager.getAllNetworks();
for(int i = 0 ; i < networks.length ; i++) {
NetworkInfo info = connectivityManager.getNetworkInfo(networks[i]);
if(info.getTypeName().equals("MOBILE")) {
// This is mobile network (3G/LTE)
// Use networks[i].bindSocket(java.net.Socket) for low-level socket communication
// Use networks[i].openConnection(java.net.URL) for HTTP request
}
else if(info.getTypeName().equals("WIFI")) {
// This is WiFi network
// Use networks[i].bindSocket(java.net.Socket) for low-level socket communication
// Use networks[i].openConnection(java.net.URL) for HTTP request
}
else {
// This is another network (WIFI_P2P, Ethernet, Bluetooth, ...)
}
}