Android的开发者在一些特定情况下都需要知道手机中的唯一设备ID。例如,跟踪应用程序的安装,生成用于复制保护的DRM时需要使用设备的唯一ID。在本文档结尾处提供了作为参考的示例代码片段。

范围

本文提供有关如何读取各种Android设备的 ID的介绍,用以使用标识号。本文假定用户已经安装了Android以及开发应用程序必要的工具。并且,本文假定用户已了解Android的基本知识。

简介在搭载Android操作系统的设备中,已经存在好几种类型的设备标识号。先前的所有Android设备都具有电话功能,因此查找每部设备硬件唯一的IMEI,MEID,或ESN也很容易。但仅能使用Wifi的设备或音乐播放器没有电话硬件,所以没有这种类型的唯一标识号。本文阐述了如何读取不同Android设备的标识号。检索Android设备ID各种方式

以下是Android设备不同类型的识别设备ID。

· 唯一编号(IMEI,MEID,ESN,IMSI)

· MAC地址

· 序列号

· ANDROID_ID

唯一编号(IMEIMEIDESNIMSI

说明在以前,当Android设备均作为电话使用时,寻找唯一标识号比较简单:()可用于找到(取决于网络技术)手机硬件唯一的IMEI,MEID,ESN和IMSI编号。

TelephonyManager.getDeviceId

IMEIMEIDESNIMSI的定义如下:

•IMEI(国际移动设备识别码)唯一编号,用于识别 GSM,WCDMA手机以及一些卫星电话(移动设备识别码)全球唯一编号,用于识别CDMA移动电台设备的物理硬件,MEID出现的目的是取代ESN号段(电子序列号)(电子序列号)唯一编号,用于识别CDMA手机(国际移动用户识别码)与所有GSM和UMTS网络手机用户相关联的唯一识别编号如需要检索设备的ID,在项目中要使用以下代码:

•MEID

•ESN

•IMSI

import android.telephony.TelephonyManager;

import android.content.Context;

String   imeistring = null;

String   imsistring = null;

{

TelephonyManager    telephonyManager;

telephonyManager =

( TelephonyManager )getSystemService( Context.TELEPHONY_SERVICE );

/*

* getDeviceId() function Returns the unique device ID.

* for example,the IMEI for GSM and the MEID or ESN for CDMA phones.

*/

imeistring = telephonyManager.getDeviceId();

/*

* getSubscriberId() function Returns the unique subscriber ID,

* for example, the IMSI for a GSM phone.

*/

imsistring = telephonyManager.getSubscriberId();

}

如要只读取手机的状态,则需添加READ_PHONE_STATE许可到AndroidManifest.xml文件中。

<uses-permission

android:name="android.permission.READ_PHONE_STATE" >

</uses-permission>

缺点

•Android设备要具有电话功能

•其工作不是很可靠

•序列号

•当其工作时,该值保留了设备的重置信息(“恢复出厂设置”),从而可以消除当客户删除自己设备上的信息,并把设备转另一个人时发生的错误。

Mac地址

说明

可通过检索找到设备的Wi - Fi或蓝牙硬件的Mac地址。但是,不推荐使用Mac地址作为唯一的标识号。

缺点设备要具备Wi – Fi功能(并非所有的设备都有Wi – Fi功能)如果设备目前正在使用Wi - Fi,则不能报告Mac地址

序列号

从Android 2.3(“姜饼”)开始,通过android.os.Build.SERIAL方法序列号可被使用。没有电话功能的设备也都需要上给出唯一的设备ID;  某些手机也可以需要这样做。序列号可以用于识别MID(移动互联网设备)或PMP(便携式媒体播放器),这两种设备都没有电话功能。通过读取系统属性值“ro.serialno”的方法,可以使用序列号作为设备ID 。如检索序列号并作为设备ID使用,请参考下面的代码示例。

import java.lang.reflect.Method;

String serialnum = null;

try {

Class<?> c = Class.forName("android.os.SystemProperties");

Method get = c.getMethod("get", String.class, String.class );

serialnum = (String)(   get.invoke(c, "ro.serialno", "unknown" )  );

}

catch (Exception ignored)

{

}

缺点

序列号无法在所有Android设备上使用。

ANDROID_ID

说明

更具体地说,Settings.Secure.ANDROID_ID 是一串64位的编码(十六进制的字符串),是随机生成的设备的第一个引导,其记录着一个固定值,通过它可以知道设备的寿命(在设备恢复出厂设置后,该值可能会改变)。 ANDROID_ID也可视为作为唯一设备标识号的一个好选择。如要检索用于设备ID 的ANDROID_ID,请参阅下面的示例代码

String androidId = Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID);

缺点

• 对于Android 2.2(“Froyo”)之前的设备不是100%的可靠

• 此外,在主流制造商的畅销手机中至少存在一个众所周知的错误,每一个实例都具有相同的ANDROID_ID。

结论

对于绝大多数应用来说,只需识别特定的安装配置,而不需要识别物理设备。所幸是,这样做就省去了麻烦。

下面是部分使用设备ID的最佳途径:

•支持各种设备类型的另一种方法是使用getDeviceID()API和ro.serialno的组合

•有许多值得参考的原因,来提醒开发者避免试图识别特定的设备。对于那些想做一下这方面尝试的用户, 最好的办法可能是使用ANDROID_ID,并在一些传统设备上做尝试。

示例代码

下面是用于追踪Android设置的示例代码

: ReadDeviceID.java

public class ReadDeviceID extends Activity {

Button bt;

TextView idView;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

bt=(Button)findViewById(R.id.button1);

idView=(TextView)findViewById(R.id.textView1);

bt.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String imeistring=null;

String imsistring=null;

TelephonyManager   telephonyManager =

( TelephonyManager)getSystemService( Context.TELEPHONY_SERVICE );

/*

* getDeviceId() function Returns the unique device ID.

* for example,the IMEI for GSM and the MEID or ESN for CDMA phones.

*/

imeistring = telephonyManager.getDeviceId();

idView.append("IMEI No : "+imeistring+"\n");

/*

* getSubscriberId() function Returns the unique subscriber ID,

* for example, the IMSI for a GSM phone.

*/

imsistring = telephonyManager.getSubscriberId();

idView.append("IMSI No : "+imsistring+"\n");

/*

* System Property ro.serialno returns the serial number as unique number

* Works for Android 2.3 and above

*/

String hwID = android.os.SystemProperties.get("ro.serialno", "unknown");

idView.append( "hwID : " + hwID + "\n" );

String serialnum = null;

try {

Class<?> c = Class.forName("android.os.SystemProperties");

Method get = c.getMethod("get", String.class, String.class );

serialnum = (String)(   get.invoke(c, "ro.serialno", "unknown" )  );

idView.append( "serial : " + serialnum + "\n" );

} catch (Exception ignored) {

}

String serialnum2 = null;

try {

Class myclass = Class.forName( "android.os.SystemProperties" );

Method[] methods = myclass.getMethods();

Object[] params = new Object[] { new String( "ro.serialno" ) , new String(

"Unknown" ) };

serialnum2 = (String)(methods[2].invoke( myclass, params ));

idView.append( "serial2 : " + serialnum2 + "\n" );

}catch (Exception ignored)

{

}

/*

* Settings.Secure.ANDROID_ID returns the unique DeviceID

* Works for Android 2.2 and above

*/

String androidId = Settings.Secure.getString(getContentResolver(),

Settings.Secure.ANDROID_ID);

idView.append( "AndroidID : " + androidId + "\n" );

}

});

}

}

: SystemProperties.java

package android.os;

/**

* Gives access to the system properties store. The system properties

* store contains a list of string key-value pairs.

*

* {@hide}

*/

public class SystemProperties

{

public static final int PROP_NAME_MAX = 31;

public static final int PROP_VALUE_MAX = 91;

private static native String native_get(String key);

private static native String native_get(String key, String def);

private static native int native_get_int(String key, int def);

private static native long native_get_long(String key, long def);

private static native boolean native_get_boolean(String key, boolean def);

private static native void native_set(String key, String def);

/**

* Get the value for the given key.

* @return an empty string if the key isn't found

* @throws IllegalArgumentException if the key exceeds 32 characters

*/

public static String get(String key) {

if (key.length() > PROP_NAME_MAX) {

throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);

}

return native_get(key);

}

/**

* Get the value for the given key.

* @return if the key isn't found, return def if it isn't null, or an empty string otherwise

* @throws IllegalArgumentException if the key exceeds 32 characters

*/

public static String get(String key, String def) {

if (key.length() > PROP_NAME_MAX) {

throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);

}

return native_get(key, def);

}

/**

* Get the value for the given key, and return as an integer.

* @param key the key to lookup

* @param def a default value to return

* @return the key parsed as an integer, or def if the key isn't found or

*         cannot be parsed

* @throws IllegalArgumentException if the key exceeds 32 characters

*/

public static int getInt(String key, int def) {

if (key.length() > PROP_NAME_MAX) {

throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);

}

return native_get_int(key, def);

}

/**

* Get the value for the given key, and return as a long.

* @param key the key to lookup

* @param def a default value to return

* @return the key parsed as a long, or def if the key isn't found or

*         cannot be parsed

* @throws IllegalArgumentException if the key exceeds 32 characters

*/

public static long getLong(String key, long def) {

if (key.length() > PROP_NAME_MAX) {

throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);

}

return native_get_long(key, def);

}

/**

* Get the value for the given key, returned as a boolean.

* Values 'n', 'no', '0', 'false' or 'off' are considered false.

* Values 'y', 'yes', '1', 'true' or 'on' are considered true.

* (case insensitive).

* If the key does not exist, or has any other value, then the default

* result is returned.

* @param key the key to lookup

* @param def a default value to return

* @return the key parsed as a boolean, or def if the key isn't found or is

*         not able to be parsed as a boolean.

* @throws IllegalArgumentException if the key exceeds 32 characters

*/

public static boolean getBoolean(String key, boolean def) {

if (key.length() > PROP_NAME_MAX) {

throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);

}

return native_get_boolean(key, def);

}

/**

* Set the value for the given key.

* @throws IllegalArgumentException if the key exceeds 32 characters

* @throws IllegalArgumentException if the value exceeds 92 characters

*/

public static void set(String key, String val) {

if (key.length() > PROP_NAME_MAX) {

throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);

}

if (val != null && val.length() > PROP_VALUE_MAX) {

throw new IllegalArgumentException("val.length > " +

PROP_VALUE_MAX);

}

native_set(key, val);

}

}

使用"ReadDeviceID" activity 创建"com.deviceid"项目。将布局"main.xml"改写成下面的代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<Button

android:text="GetDeviceID"

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

</Button>

<TextView

android:id="@+id/textView1"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

</TextView>

</LinearLayout>

"AndroidManifest.xml"文件中添加"READ_PHONE_STATE"许可,使应用程序可以登陆互联网。

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.deviceid"

android:versionCode="1"

android:versionName="1.0">

<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".ReadDeviceID"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

<uses-permission

android:name="android.permission.READ_PHONE_STATE" >

</uses-permission>

</manifest>

转载于:https://www.cnblogs.com/songmeng/p/3780832.html

Android设备的ID相关推荐

  1. 获取android设备唯一ID和用途

    获取android设备唯一ID和用途 编者:李国帅 qq:9611153 微信lgs9611153 时间:2021/5/16 获取android设备唯一ID: 在android9及之前,我们还是可以获 ...

  2. 2021年Android设备唯一ID总结(Android11.0以下)

    一.Iemi号与DeviceId Android 10.0以下都可以获取并保证设备唯一性(记的动态申请READ_PHONE_STATE权限),Android10.0以上被限制无法获取.官方文档如下: ...

  3. 最全详解Android设备UDID还是唯一ID?

    这篇文章主要介绍了Android设备UDID还是唯一ID?我觉得挺不错的,现在分享给大家,也给大家做个参考. 我想为我的 Android应用程序生成android设备唯一ID,以根据用户设备udid创 ...

  4. 新版本android_id,android手机唯一id方案总结

    google对隐私管理越来越严格了,华为也出了个OAID来保护用户隐私.对于生成android设备唯一id一直没有个绝对完美的方案,只能说做到尽量唯一吧,这里做一下总结. 一.设备系统版本为Andro ...

  5. 关于android设备唯一区分device id的取得

    2019独角兽企业重金招聘Python工程师标准>>> 有些apk为了区分唯一设备,需要用到一个device id. 1. 取得设备的MAC address    如果用户没有通过w ...

  6. android 获取蓝牙设备id_不需要任何权限获得Android设备的唯一ID

    这个问题来自于Is there a unique Android device ID? 我对这个问题的答案做了整理,包括将另一篇文章加入进来作为补充,可以完美解决此问题. 作者提出的问题: Andro ...

  7. 是否有唯一的Android设备ID?

    Android设备是否具有唯一的ID,如果是,则使用Java访问它的简单方法是什么? #1楼 有许多不同的方法可以解决这些ANDROID_ID问题(有时可能为null或特定模型的设备总是返回相同的ID ...

  8. android设备id完美解决方法,如何在Android中获取唯一的设备硬件ID?

    您可以在下面的链接中查看此博客 [http://android-developers.blogspot.in/2011/03/identifying-app-installations.html] A ...

  9. Android Q 获取设备唯一ID(UDID\GUID\UUID\SSAID\GAID)

    Android Q获取设备唯一ID(UDID\GUID\UUID\SSAID\GAID) 一.简介 1.1 问题背景 1.2 关键技术 二.解决方案 2.1 谷歌官方推荐方案 (4种) 2.2 实现方 ...

最新文章

  1. PHP 删除数组中元素的方式
  2. 赠票 | 面见AI大神贾扬清!阿里云峰会船票抢先送~(上海站)
  3. TCP/UDP的小事情
  4. python-------装饰器
  5. codeforces 118A-C语言解题报告
  6. 5.1特辑 | 为什么显示有票你却抢不到?技术揭秘12306如何保证车票不超卖
  7. python求高阶导数_TensorFlow:计算Hessian矩阵(和高阶导数)
  8. 微信灰度测试“相关阅读”功能 公众号文章下推荐延伸内容
  9. 用C#设计一个四则运算器
  10. Bailian3719 学生信息用qsort排序【排序+字符串库函数】
  11. 大数据Hadoop复习笔记
  12. 力扣-102. 二叉树的层序遍历
  13. Java基础知识汇总(持续更新)
  14. 【单目标优化求解】基于matlab混沌算法求解单目标问题【含Matlab源码 1410期】
  15. 【Java8实战】list分组、过滤、统计、排序等常用操作
  16. 中介管理系统php源代码,PHP房产中介管理系统小程序源码8.0.5 后台+前端
  17. Web前端开发和后端开发有什么区别?
  18. FPGA那些事(黑金动力社区)-笔记
  19. 高通sensor core培训笔记
  20. 【开源】STC12C5A60S2开发板

热门文章

  1. 浅析托管与非托管C++代码(转)
  2. 2019-04-01
  3. git clone --depth=1引起的问题
  4. 记一次PHP服务器500错误的解决方法
  5. 浅谈 OpenResty
  6. 在Content provider实现中使用SQLiteOpenHelper
  7. Android杂谈--ListView之BaseAdapter的使用
  8. Xna游戏编辑器开发(WinForm内嵌Xna)
  9. 计算机管理找不到指定模块,卸载时找不到指定模块怎么办_电脑卸载找不到指定模块处理方法-win7之家...
  10. 关于sql的正则表达式