[英]How to tell wifi or cellular connection using Java on Windows 10
我正在運行Windows 10 prof上的Java應用程序。 帶有SIM卡的Microsoft Surface Pro平板電腦上。 該應用程序提供了幾種更新機制來更新EFB(電子集塵袋)應用程序的集合,生成報告,將其發送到遠程Alfresco服務器,並為用戶和公司提供Doc-lifecycle。 有大小更新,這意味着一個更新處理大約300MB,另一個更新處理每個1.8 GB。 現在,在沒有wifi的情況下,我們還將實施蜂窩更新。 現在,我花了很多時間從Java角度區分wifi和蜂窩連接。 我找到了一個.net-API來完成此操作,但是我找不到對應的Java方法。 當然,我可以構建一個.net-binary並從Java調用它以存儲帶有答案的文件,但這似乎很丑陋。 任何人都具有在Windows 10上如何僅使用Java區分蜂窩和Wifi的經驗? 任何提示歡迎。
此代碼使用java.net.InterfaceAddress來檢查up接口。 從這一點開始,可以從.getDisplayName()方法輕松檢測到連接類型。 我從https://examples.javacodegeeks.com/core-java/net/networkinterface/java-net-networkinterface-example/修改了代碼
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.lang.StringUtils;
public class networkConnectionTeller {
public static boolean isNetworkRunningViaCellular() throws SocketException {
String s = "";
// NetworkInterface implements a static method that returns all the
//interfaces on the PC,
// which we add on a list in order to iterate over them.
ArrayList interfaces =
Collections.list(NetworkInterface.getNetworkInterfaces());
s += ("Printing information about the available interfaces...\n");
for (Object ifaceO : interfaces) {
NetworkInterface iface = (NetworkInterface) ifaceO;
// Due to the amount of the interfaces, we will only print info
// about the interfaces that are actually online.
if (iface.isUp() &&
!StringUtils.containsIgnoreCase(iface.getDisplayName(), "loopback")) {
//Don`t want to see software loopback interfaces
// Display name
s += ("Interface name: " + iface.getDisplayName() + "\n");
// Interface addresses of the network interface
s += ("\tInterface addresses: ");
for (InterfaceAddress addr : iface.getInterfaceAddresses()) {
s += ("\t\t" + addr.getAddress().toString() + "\n");
}
// MTU (Maximum Transmission Unit)
s += ("\tMTU: " + iface.getMTU() + "\n");
// Subinterfaces
s += ("\tSubinterfaces: " +
Collections.list(iface.getSubInterfaces()) + "\n");
// Check other information regarding the interface
s += ("\tis loopback: " + iface.isLoopback() + "\n");
s += ("\tis virtual: " + iface.isVirtual() + "\n");
s += ("\tis point to point: " + iface.isPointToPoint() + "\n");
System.out.println(s);
if (iface.getDisplayName().contains("Broadband")) {
return true;
}
}
}
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.