Android獲取設(shè)備IP地址的示例代碼
?我們在做Android平臺GB28181設(shè)備接入模塊的時(shí)候,上層需要拿到設(shè)備的IP地址,廢話不多說,分享一段獲取設(shè)備IP地址的代碼:
package com.daniulive.smartpublisher;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
public class IPAddrUtils {
/**
* 獲取本機(jī)IPv4地址
*
* @param context
* @return 本機(jī)IPv4地址;null:無網(wǎng)絡(luò)連接
*/
public static String getIpAddress(Context context) {
if (null == context)
return getIpAddress();
// 獲取WiFi服務(wù)
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
// 判斷WiFi是否開啟
if (wifiManager.isWifiEnabled()) {
// 已經(jīng)開啟了WiFi
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
return ip;
} else {
// 未開啟WiFi
return getIpAddress();
}
}
private static String intToIp(int ipAddress) {
return (ipAddress & 0xFF) + "." +
((ipAddress >> 8) & 0xFF) + "." +
((ipAddress >> 16) & 0xFF) + "." +
(ipAddress >> 24 & 0xFF);
}
/**
* 獲取本機(jī)IPv4地址
*
* @return 本機(jī)IPv4地址;null:無網(wǎng)絡(luò)連接
*/
public static String getIpAddress() {
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()
&& !inetAddress.isAnyLocalAddress()
&& inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (Exception e) {
}
return null;
}
}
調(diào)用非常簡單:
String local_ip_addr = IPAddrUtils.getIpAddress(context_); Log.i(TAG, "initGB28181Agent local ip addr: " + local_ip_addr);
當(dāng)然有些場景下,不一定可以拿到設(shè)備IP,這時(shí)候,需要手動設(shè)置IP地址到模塊。
方法補(bǔ)充:
除了上文的方法,小編還為大家整理了其他Android 獲取設(shè)備IP的方法,希望對大家有所幫助
獲取IP
使用WIFI聯(lián)網(wǎng)與不使用WIFI,獲取到的IP地址不同。因此,需要先判斷當(dāng)前設(shè)備通過哪種方式聯(lián)網(wǎng),然后再獲取對應(yīng)的IP地址。
判斷網(wǎng)絡(luò)連接類型
通過ConnectivityManager判斷網(wǎng)絡(luò)連接類型,代碼如下:
private fun checkCurrentNetworkType() {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.run {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
when (activeNetworkInfo?.type) {
ConnectivityManager.TYPE_MOBILE -> {
// 通過手機(jī)流量
}
ConnectivityManager.TYPE_WIFI -> {
// 通過WIFI
}
else -> {}
}
} else {
// Android M 以上建議使用getNetworkCapabilities API
activeNetwork?.let { network ->
getNetworkCapabilities(network)?.let { networkCapabilities ->
if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
when {
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
// 通過手機(jī)流量
}
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
// 通過WIFI
}
}
}
}
}
}
}
}
獲取手機(jī)卡聯(lián)網(wǎng) IP
通過NetworkInterface獲取IPV4地址,代碼如下:
NetworkInterface.getNetworkInterfaces().let {
loo@ for (networkInterface in Collections.list(it)) {
for (inetAddresses in Collections.list(networkInterface.inetAddresses)) {
if (!inetAddresses.isLoopbackAddress && !inetAddresses.isLinkLocalAddress) {
// IP地址
val mobileIp = inetAddresses.hostAddress
break@loo
}
}
}
}
獲取WIFI聯(lián)網(wǎng) IP
通過ConnectivityManager或WifiManager來獲取IP地址,代碼如下:
private fun getWIFIIp() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
// IP 地址
val wifiIp = Formatter.formatIpAddress(wifiManager.connectionInfo.ipAddress)
} else {
// Android Q 以上建議使用getNetworkCapabilities API
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.run {
activeNetwork?.let { network ->
(getNetworkCapabilities(network)?.transportInfo as? WifiInfo)?.let { wifiInfo ->
// IP 地址
val wifiIp = Formatter.formatIpAddress(wifiInfo.ipAddress)
}
}
}
}
}
到此這篇關(guān)于Android獲取設(shè)備IP地址的示例代碼的文章就介紹到這了,更多相關(guān)Android獲取IP地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實(shí)現(xiàn)
這篇文章主要介紹了Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Android中使用RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載
RecyclerView 是Android L版本中新添加的一個(gè)用來取代ListView的SDK,它的靈活性與可替代性比listview更好。這篇文章主要介紹了Android中使用RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載的相關(guān)資料,需要的朋友可以參考下2016-03-03
android FM播放時(shí)拔出耳機(jī)后FM APP自動close解決方法
android FM播放時(shí)拔出耳機(jī)后FM APP自動close關(guān)閉的情況應(yīng)該怎樣解決呢?下面為大家詳細(xì)介紹下具體修改方法,感興趣的朋友可以參考下2013-06-06
Android使用Recyclerview實(shí)現(xiàn)圖片水平自動循環(huán)滾動效果
這篇文章主要為大家詳細(xì)介紹了Android使用Recyclerview實(shí)現(xiàn)圖片水平自動循環(huán)滾動效果,實(shí)現(xiàn)精彩的跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android集成GreenDao數(shù)據(jù)庫的操作步驟
這篇文章主要介紹了Android集成GreenDao數(shù)據(jù)庫,使用數(shù)據(jù)庫存儲時(shí)候,一般都會使用一些第三方ORM框架,比如GreenDao,本文分幾步給大家介紹Android集成GreenDao數(shù)據(jù)庫的方法,需要的朋友可以參考下2022-10-10

