使用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