我尝试获取可用网络的列表,但是当WiFi启用时,尽管启用了GSM / LTE,但我只能看到“ WIFI”。 是否有一种获取可用网络列表的好方法?
我正在尝试这样做:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
String strNetworkName = "";
if(null != connManager) {
Network networks[] = connManager.getAllNetworks();
if(networks != null) {
NetworkInfo nwInfo = null;
for(Network nw: networks) {
nwInfo = connManager.getNetworkInfo(nw);
strNetworkName += nwInfo.getTypeName() + ", ";
}
}
}
Toast.makeText(getApplicationContext(), strNetworkName, Toast.LENGTH_SHORT).show();
答案 0 :(得分:0)
我开发了返回连接名称和网络Map的函数。 我认为这可能很有趣。
Map<String,Network> getAvailableNetwoks(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
HashMap<String,Network> setNetworkName = new HashMap<String,Network>();
if (null != connManager) {
// Set MOBILE network for checking is it existing
activateMobileNetwork(context, connManager);
Network networks[] = connManager.getAllNetworks();
if (networks != null) {
NetworkInfo nwInfo;
for (Network nw : networks) {
nwInfo = connManager.getNetworkInfo(nw);
setNetworkName.put(nwInfo.getTypeName(), nw);
}
}
}
return setNetworkName;
}
void activateMobileNetwork(Context context, ConnectivityManager connManager) {
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
NetworkRequest networkRequest = builder.build();
connManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
Toast.makeText(context, "MOBILE connect", Toast.LENGTH_SHORT).show();
}
});
}