Android模拟器的设备标识符
我想在模拟器中测试依赖于设备标识符(ANDROID_ID)的应用程序。
我目前使用以下代码获取设备标识符:
final String deviceID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
当我在模拟器中运行它时,它返回 null
,这给我带来了各种各样的问题。似乎更高的 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);
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在模拟器中,IMEI 和 IMSI 的值为 硬编码:
因此,您将始终得到
null
。如果您仍然想要使用这些ID号进行测试,并且想要为模拟器和真实设备保留相同的代码,则必须以某种方式在模拟器中更改它。
至少有两种方法可以做到这一点:
更改代码中的值并重新编译模拟器的代码。但是,这可能太复杂且耗时...:-)
“破解”模拟器二进制文件(因为它既没有压缩也没有加密 - 你可以做到!)并修改字符串(在正确的位置)就在那里。
具体操作方法如下:
备份模拟器二进制文件(稍后回滚!)。在 Windows 中,可以在 Android“\tools”文件夹中的“emulator.exe”名称下找到该二进制文件。
使用您最喜欢的十六进制编辑器打开二进制文件
搜索后跟空字节的+CGSN字符串(后面应该是15 位 IMEI 号码 - 请参阅下面的打印屏幕)
编辑数字(注意不要改变原来的位数)
并保存文件!
也许可以更改/调整您的代码以使用您的 ID 的 IMEI(如
Falmari
指出的那样),或者使用此技巧来更改一些其他值。In the emulator, the values of IMEI and IMSI are hardcoded:
therefore, you will always get
null
.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.
There are at least two ways how to do it:
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.
Here's how to do it:
backup the emulator binary (to roll back! later). In Windows, the binary can be found under the name "emulator.exe", located in your android "\tools" folder.
open the binary with your favourite hex editor
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)
and save the file!
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.如果您想要非空模拟器 uuid,请像这样启动模拟器:
If you want non-null emulator uuid, then start the emulator like this:
正如 Falmarri 所说,模拟器中的设备 ID 将为 0。
我使用这种方法根据参数组合生成一个唯一的设备 ID(它似乎对我有用,尽管我没有广泛测试它 - 模拟器和 HTC Desire) - 这不是我的方法(我不记得了)我在哪里挖出来的 - 但归因于它应有的位置)
希望这会有所帮助。
As Falmarri says, the device Id will be 0 in the emulator.
I use this method to generate a unique device Id based on a combination of parameters (it seems to work for me although I haven't tested it extensively - the emulator and an HTC Desire) - it's not my method (I can't remember where I dug it up - but attribution where it's due)
Hope this helps.
可以破解模拟器二进制文件以放入替代值。但是,它必须以十进制数字开头,因为在 reference-ril.c 中,它调用
at_send_command_numeric()
来读取该值。我认为必须更改为at_send_command_singleline()
以支持 MEID 字符串(通常是以“A”开头的 14 个十六进制数字)。除非您真的很聪明并且可以在二进制文件中找到并交换函数地址,否则您必须在修补它之后从源代码构建以使用某些手机具有的相同值。It's fine to hack the emulator binary to put in an alternate value. However, it must start with a decimal digit because in reference-ril.c, it calls
at_send_command_numeric()
to read the value. I believe that has to be changed toat_send_command_singleline()
to support MEID strings (that are typically 14 hex digits starting with 'A'). Unless you're really clever and can find and swap the function addresses in the binary, you'll have to build from source after patching it to use the same value that some phones have.