Android 模拟器的设备标识符 [英] Device identifier of Android emulator

查看:58
本文介绍了Android 模拟器的设备标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在模拟器中测试一个依赖于设备标识符 (ANDROID_ID) 的应用.

I want to test in the emulator an app that depends of the device identifier (ANDROID_ID).

我目前使用以下代码获取设备标识符:

I currently obtain device identifier with the following code:

final String deviceID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

当我在模拟器中运行它时,它返回 null,这给我带来了各种各样的问题.似乎更高的 Android 版本会返回一些东西.

When I run this in an emulator it returns null, which gives me all sort of problems. It seems that higher Android versions it returns something.

有没有办法在 Android 模拟器中获取设备标识符?我是否错误地获取了设备 ID?

Is there a way to get a device identifier in the Android emulator? Am I obtaining the device id wrongly?

也许可以通过控制台设置模拟器的设备标识符?

Maybe it's possible to set the device identifier of the emulator through the console?

推荐答案

在模拟器中,IMEI和IMSI的值为硬编码:

In the emulator, the values of IMEI and IMSI are hardcoded:

2325     { "+CIMI", OPERATOR_HOME_MCCMNC "000000000", NULL },   /* request internation subscriber identification number */
2326     { "+CGSN", "000000000000000", NULL },   /* request model version */

因此,您将始终获得 null.

therefore, you will always get null.

如果您仍然想将这些 ID 号用于您的测试,并且您想为模拟器和真实设备保留相同的代码,您必须以某种方式在模拟器中更改它.

If you still want to use these id numbers for your testing and you want to keep the same code for the emulator and the real device, you must change it in the emulator somehow.

至少有两种方法可以做到:

  1. 更改代码中的值并为模拟器重新编译代码.但是,这可能太复杂和耗时... :-)

  1. Change the values in the code and recompile the code for the emulator. However, this might be too complicated and time consuming... :-)

破解"模拟器二进制文件(因为它既没有压缩也没有加密——你可以做到!)并在那里修改字符串(在正确的位置).

"Hack" the emulator binary (since it is neither compressed or encrypted - you can do it!) and modify the strings (in the right place) right there.

操作方法如下:

  • 备份模拟器二进制文件(回滚!稍后).在 Windows 中,二进制文件可以在名称emulator.exe"下找到,位于您的 android ools"文件夹中.

  • backup the emulator binary (to roll back! later). In Windows, the binary can be found under the name "emulator.exe", located in your android " ools" folder.

用你最喜欢的十六进制编辑器打开二进制文件

open the binary with your favourite hex editor

搜索 +CGSN 字符串后跟一个空字节(后跟 15 位 IMEI 号码 - 请参阅下面的打印屏幕)

search for the +CGSN string followed by a null byte (it should be followed by 15 digits of the IMEI number - see the printscreen below)

  • 编辑号码(注意不要改变原来的数字)

  • edit the number (be careful not to change the original number of the digits)

并保存文件!

也许可以更改/调整您的代码以使用 IMEI 作为您的 ID(正如 Falmari 指出的那样),或者使用此技巧来更改其他一些值.

and maybe change/adjust your code to use the IMEI for your id (as Falmari points out), or use this trick to change some other values.

这篇关于Android 模拟器的设备标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆