android studio|Android 模拟器 Emulator 设置地理位置信息的几个方法

更新时间:2019-10-03    来源:.Net开发    手机版     字体:

【www.bbyears.com--.Net开发】

使用Android Emulator,可以自己设置GPS地理位置信息,根据Android官方文档的介绍,可以有如下几种方式:

1. 通过命令行:

telnet 5554    # 5554为emulator的console端口
 
geo fix 121.420413 31.215345  # 第一个数值是经度(longitude),第二个数值是纬度(latitude)
# 可从 google maps获取经纬度, https://maps.google.com/

2. 通过Eclipse:


设置路径为:Window > Show View > Other > Emulator Control.
In the Emulator Control panel, enter GPS coordinates under Location Controls as individual lat/long coordinates, with a GPX file for route playback, or a KML file for multiple place marks. (Be sure that you have a device selected in the Devices panel—available from Window > Show View > Other > Devices.)
Using DDMS

3. 通过DDMS:

With the DDMS tool, you can simulate location data a few different ways:
Manually send individual longitude/latitude coordinates to the device.
Use a GPX file describing a route for playback to the device.
Use a KML file describing individual place marks for sequenced playback to the device.

我还是比较喜欢用命令行的方式~
不过,我通常使用HAXM或KVM来加速我的emulator(里面时运行Intel的image),我就发现这些设置方法对我的emulator都没有生效。这个问题曾经困扰了我好几天,后来和一个同事交流时才发现,其实这个设置地理位置信息是依赖于Google API的,而使用Intel的一些image,里面默认时没有Google API的,所以不能生效(即使geo fix命令返回是ok)。所幸的是,Android SDK中从android 4.4开始也直接提供了x86 image的Google APIs。
对于Android 4.3/4.2等的X86镜像,需要通过如下步骤来手动添加Google API的支持。

 代码如下 1.  In Android Virtual Device Manager create an AVD with target "Android 4.3/4.2"  (其实就是先使用arm的image)
2.    emulator -avd name_of_avd    (启动arm image)
3.   adb pull /system/etc/permissions/com.google.android.maps.xml     (下载google maps相关文件)
4.    adb pull /system/framework/com.google.android.maps.jar   (下载google maps相关文件)
5.    (optional) Remove the create AVD in Android Virtual Device Manager   (删掉刚才使用的ARM avd,可选)
6.   In Android Virtual Device Manager create an AVD with target "Intel Atom x86 system Image (Intel Corporation) - API Level 18"    (创建Intel x86 image)
8.   emulator -partition-size 1024 -no-snapshot-save -avd name_of_avd   (启动x86 image)
9.   adb remount rw    (让文件系统可写)
10.    adb push com.google.android.maps.xml /system/etc/permissions    (上传google maps相关文件到x86 iamge中)
11.   adb push com.google.android.maps.jar /system/framework   (上传google maps相关文件到x86 iamge中)
12.    Download mkfs.yaffs2.x86  (下载地址:https://android-group-korea.googlecode.com/files/mkfs.yaffs2.x86)
13.   adb push mkfs.yaffs2.x86 /data   (将mkfs.yaffs2.x86传到image里面)
14.   adb shell   (连接到该emualtor的shell)
15.   cd /data
16.    chmod 777 mkfs.yaffs2.x86
17.   ./mkfs.yaffs2.x86 /system system.img            (重新制作system.img文件)
18.    exit  (退出adb shell)
19.    adb pull /data/system.img  (下载制作好的system.img,需要较长时间,耐心等待吧)
20.    Copy system.img into avd directory    (将这个system.img 复制到的你的AVD目录中,覆盖掉原来的system.img,可以先备份原来的)
21.   Reboot emulator  (重启emulator,即可使用到新的system.img)



我第一次这么操作时,在上传google maps文件到x86 image中时,出现了如下的错误:

jay@jay-linux:~/adt-bundle-linux-x86_64-20140321/sdk$ adb push com.google.android.maps.xml /system/etc/permissions
failed to copy "com.google.android.maps.xml" to "/system/etc/permissions/com.google.android.maps.xml": Out of memory

后来发现原因是,原本的system.img的容量只够存放自己的东西,里面已经存不下其他东西了。所以,在启动x86 AVD时,要用”emulator -partition-size 1024 avd-for-x86“命令来调整/data目录的大小为1024MB。

参考资料:


android官方文档:http://developer.android.com/guide/topics/location/strategies.html
Intel image使用Google Maps API: http://38911bytes.blogspot.de/2012/03/how-to-use-google-maps-api-in-android.html
http://stackoverflow.com/questions/9857325/google-maps-sdk-with-new-intel-atom-x86-emulator

本文来源:http://www.bbyears.com/asp/70852.html

热门标签

更多>>

本类排行