編輯:關於Android編程
public static String getLocalIpAddress(){
try{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
}catch (SocketException e) {
// TODO: handle exception
Utils.log("WifiPreference IpAddress---error-" + e.toString());
}
return null;
}
但是在4.0 下 會出現類似fe80::b607:f9ff:fee5:487e的IP地址, 這個是IPV6的地址,我們需要獲得是的IPV4的地址,所以要在上訴代碼中加一個判斷
InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())
完整代碼如下:
public static String getLocalIpAddress(){
try{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
return inetAddress.getHostAddress().toString();
}
}
}
}catch (SocketException e) {
// TODO: handle exception
Utils.log("WifiPreference IpAddress---error-" + e.toString());
}
return null;
}
Android計時器的三種實現方式(Chronometer、Timer、handler)
本文實例為大家分享了Android計時器的三種方法,具體內容如下目錄:1、借助Timer實現2、調用handler.sendMessagedely(Message msg
Android 開機自啟動示例程序
Android 開機自啟動示例程序。使用廣播方式接受,采用Android自帶存儲SharedPreferences存儲開機自啟動的設置。本文源碼:點擊1、先加上權限&nb
Android ApiDemos導入
Android SDK中自帶有很多例子,存放在android-sdksamples目錄下,其中介紹了Android平台主要的API的使用,是一份不可多得的學習資料!以An
一步步教你寫Slack的Loading動畫
項目地址:https://github.com/JeasonWong/SlackLoadingView老規矩,先上效果。圖好大。。說下第一眼看到這個動畫後的思路:+兩根平