最近在做一個 Android 項目,需要用到GPS獲取位置信息,從 API 查了一下,發現獲取位置信息僅需極其簡單的一句即可:
getLastKnownLocation(LocationManager.GPS_PROVIDER),
於是高興地不得了。可是一寫進程式碼裡,回傳值(Location 類型)居然一直為null..鬱悶的不得了。在網路上查了好久,發現好多人都跟我一樣糾結於這個問題上,有人說是因為GPS沒打開,也有人說是相關權限沒加上..可是我的明明已經在設定裡打開,權限自然也加上了。在api上糾結了半天,終於找出原因了,原來要打開GPS其實在於這句:
setTestProviderEnabled("gps",true);
而跟手機上的設定沒什麼關係(起碼在我的手機上測是這樣的)。手機上的設定關閉了,這一句照樣能打開;而即使手機設定打開了,沒這一句也是白搭。與此句對應的是
setTestProviderEnabled("gps",false);
用來關閉GPS.
GPS開啟後可以用上面的方法取得Location了嗎?還是不行!確切地說是有時候可以,因為這個函數獲取的是上次已經獲得的位置信息,設想如果此程序第一次跑,先前並沒有獲取過位置信息,當然返回值為null了。經仔細查看api,在
requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
裡發現了這樣一句話:It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownation(String) methodation(String) methodation(String). ,應該用此方法為manager設定監聽器,在監聽器中onLocationChanged(Location location)裡取得。
測試程式碼如下:
public void onLocationChanged(Location location) { Log.i("onLocationChanged", "come in"); if (location != null) { Log.w("Location","Current altitude = "+ location.getAltitude()); Log.w("Location","Current latitude = "+ location.getLatitude()); } }
經過測試,經過一段時間後可以取得Location(取得時間與minTime、minDistance相關)。還需注意的一個問題是在設定了監聽器後,刪除監聽器之前不能用上面的方法關閉gps,否則會報錯。因此關閉gps的方法是
manager.removeUpdates (listener);//listener 即为监听器实例 manager.setTestProviderEnabled("gps",false);
以下是測試程式碼,所需權限有:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
import android.app.Activity; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; public class audio extends Activity { /** Called when the activity is first created. */ LocationManager locationManager; LocationListener llistener; String provider; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String serviceName = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(serviceName); locationManager.setTestProviderEnabled("gps", true); provider = locationManager.getBestProvider(criteria, true); Log.d("provider", provider); llistener = new LocationListener() { @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub Log.i("onLocationChanged", "come in"); if (location != null) { Log.w("Location", "Current altitude = " + location.getAltitude()); Log.w("Location", "Current latitude = " + location.getLatitude()); } locationManager.removeUpdates(this); locationManager.setTestProviderEnabled(provider, false); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub Log.i("onProviderDisabled", "come in"); } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub Log.i("onProviderEnabled", "come in"); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub Log.i("onStatusChanged", "come in"); } }; locationManager.requestLocationUpdates(provider, 1000, (float) 1000.0, llistener); } protected void onDestroy() { locationManager.removeUpdates(llistener); locationManager.setTestProviderEnabled(provider, false); super.onDestroy(); }
更多Android開啟GPS導覽並取得位置資訊回傳null解決方案相關文章請關注PHPC!