Android下获取设备唯一标识(UDID, DeviceID...)

android下获取设备唯一标识原本非常简单(至少不会像iOS一样禁用这个,禁用那个),但是由于设备的多样性需要考虑的东西也对应复杂起来。

先附上完整代码

 

[java]  view plain copy
 
  1. protected static final String PREFS_FILE = "gank_device_id.xml";    
  2.     protected static final String PREFS_DEVICE_ID = "gank_device_id";  
  3.     protected static String uuid;  
  4.     static public String getUDID()  
  5.     {  
  6.         if( uuid ==null ) {  
  7.             synchronized (GankMainActivity.class) {    
  8.                 if( uuid == null) {    
  9.                     final SharedPreferences prefs = s_instance.getSharedPreferences( PREFS_FILE, 0);    
  10.                     final String id = prefs.getString(PREFS_DEVICE_ID, null );    
  11.     
  12.                     if (id != null) {    
  13.                         // Use the ids previously computed and stored in the prefs file    
  14.                         uuid = id;    
  15.                     } else {    
  16.     
  17.                         final String androidId = Secure.getString(s_instance.getContentResolver(), Secure.ANDROID_ID);    
  18.     
  19.                         // Use the Android ID unless it's broken, in which case fallback on deviceId,    
  20.                         // unless it's not available, then fallback on a random number which we store    
  21.                         // to a prefs file    
  22.                         try {    
  23.                             if (!"9774d56d682e549c".equals(androidId)) {    
  24.                                 uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")).toString();    
  25.                             } else {    
  26.                                 final String deviceId = ((TelephonyManager) s_instance.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();    
  27.                                 uuid = deviceId!=null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString() : UUID.randomUUID().toString();    
  28.                             }    
  29.                         } catch (UnsupportedEncodingException e) {    
  30.                             throw new RuntimeException(e);    
  31.                         }    
  32.     
  33.                         // Write the value out to the prefs file    
  34.                         prefs.edit().putString(PREFS_DEVICE_ID, uuid).commit();    
  35.                     }  
  36.                 }    
  37.             }    
  38.         }  
  39.         return uuid;  
  40.     }  



 

1、正常情况下可以通过((TelephonyManager) s_instance.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();  来获取,但是某些平板电脑此函数会返回空

2、通过 Secure.getString(s_instance.getContentResolver(), Secure.ANDROID_ID);   也可以获取到一个id,但是android2.2或者是某些山寨手机使用这个也是有问题的,它会返回一个固定的值 9774d56d682e549c

3、如果前两个都没有获取到udid,那么就在程序启动的时候创建一个随机的uuid,然后保存起来。这个算是兼容方案,当然这样的设备并不会很多。

转载于:https://www.cnblogs.com/Nbox1989/p/4346347.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用 Python 的网络库,如 `socket` 或 `requests` 库,来获取指定 URL 的 IP 地址。 使用 `socket` 库: ``` import socket def get_ip_address(host): return socket.gethostbyname(host) host = "udid.iosyuan.com" print(get_ip_address(host)) ``` 使用 `requests` 库: ``` import requests def get_ip_address(host): return requests.get("https://" + host).raw._connection.sock.getpeername()[0] host = "udid.iosyuan.com" print(get_ip_address(host)) ``` 上面的代码将会返回该 URL 的 IP 地址。 ### 回答2: 要通过Python获取https://udid.iosyuan.com的真实IP,我们可以使用socket库和requests库来实现。 首先,我们需要使用requests库发送GET请求到https://udid.iosyuan.com,并获取网页的源代码。可以使用以下代码进行请求和获取响应的过程: ```python import requests url = "https://udid.iosyuan.com" response = requests.get(url) source_code = response.text ``` 接下来,我们可以使用正则表达式来提取源代码中的IP地址。由于具体的源代码结构不确定,这里我们只考虑提取合法的IP地址。可以使用以下代码来实现正则表达式匹配: ```python import re ip_pattern = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ip_list = re.findall(ip_pattern, source_code) ``` 最后,我们可以输出提取到的IP地址。完整的代码如下: ```python import requests import re url = "https://udid.iosyuan.com" response = requests.get(url) source_code = response.text ip_pattern = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ip_list = re.findall(ip_pattern, source_code) for ip in ip_list: print(ip) ``` 这样我们就可以使用Python获取https://udid.iosyuan.com的真实IP地址了。注意,由于网页的结构可能会发生变化,以上代码可能需要根据实际情况进行调整。 ### 回答3: 使用Python获取https://udid.iosyuan.com的真实IP地址可以通过以下步骤实现: 1. 首先,需要导入所需的库,包括`requests`和`socket`库。可以使用`pip install requests`和`pip install socket`命令进行安装。 2. 创建一个函数来获取真实IP地址,命名为`get_real_ip()`。 3. 在函数中,使用`requests.get()`函数发送一个GET请求到`https://udid.iosyuan.com`网站。 4. 然后,通过`response.text`属性获取响应的HTML内容。 5. 接下来,使用`socket.getaddrinfo()`函数来解析HTML内容中的主机名,获取其对应的IP地址。 6. 最后,将获取到的IP地址返回给调用函数的部分。 下面是一个示例代码: ```python import requests import socket def get_real_ip(): url = 'https://udid.iosyuan.com' response = requests.get(url) html_content = response.text host_name = html_content.split()[2].split("/")[2] ip = socket.getaddrinfo(host_name, None)[0][4][0] return ip # 调用函数并打印真实IP地址 ip_address = get_real_ip() print("真实IP地址是:" + ip_address) ``` 以上代码通过发送GET请求获取网页内容,并从中解析出IP地址。请注意,`getaddrinfo()`函数可能会返回多个IP地址,我们只需使用第一个即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值