可以使用Android提供的ConnectivityManager类中的getActiveNetworkInfo方法来获取当前网络连接的信息,并从中获取域名。
具体代码示例如下:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
if (dhcpInfo != null) {
String domainName = dhcpInfo.domainName;
if (domainName != null && !domainName.isEmpty()) {
// 域名
}
}
}