简体   繁体   English

android - 唯一的设备ID

[英]android - unique device ID

Yes I took a look at all similar questions, however I found no suitable answer. 是的,我看了所有类似的问题,但是我找不到合适的答案。 I need the device ID to set test devices for AdMob. 我需要设备ID来为AdMob设置测试设备。 My 1st approach ( some digits changed due to security reasons ): 我的第一种方法( 由于安全原因,一些数字发生了变化 ):

  final TelephonyManager tm = (TelephonyManager) c
            .getSystemService(Context.TELEPHONY_SERVICE);
   Log.w("ID", tm.getDeviceId());

The result: 358948074051795 结果: 358948074051795

Documentation says: Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. 文档说明: 返回唯一的设备ID,例如,GSM的IMEI和CDMA手机的MEID或ESN。 Return null if device ID is not available. 如果设备ID不可用,则返回null。

My 2nd approach: 我的第二种方法:

   Log.w("ID_1",
            Secure.getString(c.getContentResolver(), Secure.ANDROID_ID));

The result: 98ade639139bf376 结果: 98ade639139bf376

Description: A 64-bit number (as a hex string) that is randomly generated on the device's first boot and should remain constant for the lifetime of the device 描述: 在设备首次启动时随机生成的64位数字(作为十六进制字符串),应在设备的生命周期内保持不变

The question: what is the difference? 问题:有什么区别? Which one should I use for AdMob test purposes? 我应该将哪一个用于AdMob测试目的?

The difference is written in the descriptions of both methods... 差异写在两种方法的描述中......
However, I think using IMEI number is much more reliable because it identifies the device itself. 但是,我认为使用IMEI number更可靠,因为它识别设备本身。 What if the device has to restore default configuration ? 如果设备必须恢复默认配置怎么办?
You'd have to check if it doesn't generate a new number if you take your 2nd option. 如果你选择第二个选项,你必须检查它是否没有生成新的数字。
More information about IMEI can be found here : http://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity 有关IMEI的更多信息,请访问: http//en.wikipedia.org/wiki/International_Mobile_Equipment_Identity

1.Code 1.Code

Settings.Secure#ANDROID_ID returns the Android ID as an unique 64-bit hex string

import android.provider.Settings.Secure;

String deviceId = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);

Toast.makeText(this, deviceId, Toast.LENGTH_SHORT).show();

2.manifest 2.manifest

Add uses-permission in manifest file 在清单文件中添加uses-permission

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM