1. 除了前面提到的主要java模組代碼,還需要在檔案AndroidManifest.xml檔案中修改許可權:
在uses-permission中,代碼如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zfy.mygps" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".Mygps" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/></manifest>
2. 在運行中若要在模擬器中運行,還需要模擬器支援GPS,這一點開啟模擬器的時候不一定開啟了,模擬器對GPS的支援設定如下:
Eclipse軟體中:window--->android sdk and avd manager ,開啟模擬器管理視窗,如果已經有可用的模擬器,則開啟edit,如果沒有就建立一個模擬器,如:
然後在HardWare右邊的視窗中可以看到是否有對GPS的支援,如果沒有,則點擊右邊的new,彈出如下的視窗:
從中選擇對GPS的支援,然後ok退出。
這樣模擬器就支援GPS了,可以利用模擬器來測試你的程式,
3.利用Eclipse中的Emulator Control來測試定位;
window-->show view --> Emulator Control(如果在show view當中沒有Emulator Control,則在other菜單中),如果沒有開啟模擬器,則emulator control中為灰色,不能編輯,如果模擬器開啟之後還是為灰色,則重啟eclipse(模擬器不要關)
之前網上有資料說gps在android2.2或以下才支援,在android2.2和android2.3中都測試通過。