DeviceId() 显示空指针?

发布于 2024-12-09 17:30:48 字数 374 浏览 1 评论 0原文

我正在开发一个应用程序,因为我只需要设备的 IMIE 号码...

我尝试过:

telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        imie = telephonyManager.getDeviceId();

在 Menifyst 中:

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

让我告诉我,是否需要执行任何其他操作?

提前致谢。

i am developing an app in that i just need IMIE number of device...

I tried:

telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        imie = telephonyManager.getDeviceId();

In Menifiest:

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

Let me tell, if that require any other things to do??

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澉约 2024-12-16 17:30:48

我认为你做得对,但根据 Android开发者博客

我们已经看到了一些生产手机的实例,其实现存在错误并返回垃圾,例如零或星号。

此外,它可能无法在模拟器上运行。

I think you're doing it right, but according to the Android Developers Blog:

We have seen a few instances of production phones for which the implementation is buggy and returns garbage, for example zeros or asterisks.

Also, it probably won't work on an emulator.

烟酉 2024-12-16 17:30:48

这不是设备的 IMIE 号码,但如果您只需要设备的标识符,您也可以尝试 Build.SERIAL。与 getDeviceId() 类似,该值可能并不总是唯一或可用,但它是第一个方法失败时尝试的另一个选项。

This is not the IMIE number for the device but if you just need an identifier for the device, you can also try Build.SERIAL. Like getDeviceId() the value might not always be unique or available but it's another option to try when the first method fails.

秋心╮凉 2024-12-16 17:30:48

以下可能无法在模拟器上正常工作。

TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String szImei = TelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE

现在有时需要从非电话设备(例如平板电脑)获取唯一编号,此时我们可以创建伪唯一 ID,该 ID 适用于所有 Android 设备,它提供像 IMIE 这样的唯一编号。以这种方式计算的 ID 不会是唯一的:可以找到具有相同 ID 的两个设备(基于相同的硬件和 ROM 映像),但在实际应用中的机会可以忽略不计。为此,您可以使用 Build 类:

String PUID = Build.BOARD.length()%10+ Build.BRAND.length()%10 +
            Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
            Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
            Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
            Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
            Build.TAGS.length()%10 + Build.TYPE.length()%10 +
            Build.USER.length()%10 ;

PUID 将返回 13 位数字,我们在前面添加两个数字 (35),以具有与 IMEI(15 位数字)相同大小的 ID。

 PUID = "35" + PUID;

很棒的想法是我们不需要使用上面的代码添加 READ_PHONE_STATE 权限。

如需生成唯一编号的更多帮助,请访问 pocket magic

following may not work properly on emulator..

TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String szImei = TelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE

Now sometimes its require to get the unique number from non-telephonic device (e.g Tablet) at that time we can create Pseudo-Unique ID, that works on all Android devices which gives unique number like IMIE. The ID computed in this way won't be unique: it is possible to find two devices with the same ID (based on the same hardware and rom image) but the chances in real world applications are negligible. For this purpose you can use the Build class:

String PUID = Build.BOARD.length()%10+ Build.BRAND.length()%10 +
            Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
            Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
            Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
            Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
            Build.TAGS.length()%10 + Build.TYPE.length()%10 +
            Build.USER.length()%10 ;

PUID will return 13 digits number and we are adding two more in front (35) to have the same size ID like the IMEI (15 digits).

 PUID = "35" + PUID;

and great think is that we do not need to add READ_PHONE_STATE permission using above code.

For more help in generating unique number visit pocket magic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文