2014-10-28 61 views
2

我目前正在開發一個使用android.location.API的android應用程序。某些手機的Android GPS位置請求失敗

通常情況下,GPS位置請求成功有些手機(銀河注2),但失敗(getLastKnownLocation()返回null)在其他國家(銀河S3和S4)。

如果我第一次激活三星內置功能「GPS衛星視圖」,它將掃描衛星可用,這是奇怪的是,GPS工作S3 S3 & S4。

我的猜測是,這兩個S3 & S4使用聯發芯片,其提供差的GPS信號。那麼有沒有人知道如何執行相同的功能,如在Android掃描衛星?我的應用程序是在中國使用

注意,所以請不要提出任何谷歌相關服務。

謝謝。

public class MyLocationManager implements android.location.LocationListener{ 

    private static MyLocationManager instance = null; 
    private Location currentLocation; 
    private String provider; 
    private LocationManager locationManager; 
    private Context context; 

    public static MyLocationManager getInstance(){ 
     if (instance == null){ 
      instance = new MyLocationManager(); 
     } 
     return instance; 
    } 

    public boolean hasLocation(){ 
     if(currentLocation == null){ 
      return false; 
     } else{ 
      return true; 
     } 
    } 

    public float distanceInMetersFromLocation(double latitude, double longitude){ 
     if (currentLocation == null){ 
      return -1; 
     } 
     float[] results = new float[3]; 
     Location.distanceBetween(currentLocation.getLatitude(), currentLocation.getLongitude(), latitude, longitude, results); 

     return results[0]; 
    } 


    public void startListenLocation(Context context) { 

     this.context = context; 

     locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
     boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     boolean wifiEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (gpsEnabled){ 
      provider = LocationManager.GPS_PROVIDER; 
      Log.e("MyLocationManager", "GPS"); 
     } else if (wifiEnabled){ 
      provider = LocationManager.NETWORK_PROVIDER; 
      Log.e("MyLocationManager", "NetWork"); 
     } else { 
      Log.e("MyLocationManager", "Please Enable Location Service"); 
      return; 
     } 

     requestLocationUpdates(); 

     if(provider.equals(LocationManager.GPS_PROVIDER)){ 
      currentLocation = locationManager.getLastKnownLocation(provider); 
     } else{ 
      currentLocation = locationManager.getLastKnownLocation(provider); 
     } 

     if(currentLocation == null && provider.equals(LocationManager.GPS_PROVIDER)){ 
      Log.e("MyLocationManager", "GPS Failed"); 
      if(wifiEnabled){ 
       Log.e("MyLocationManager", "Use Wifi"); 
       provider = LocationManager.NETWORK_PROVIDER; 
       requestLocationUpdates(); 
       currentLocation = locationManager.getLastKnownLocation(provider); 
      }else{ 
       return; 
      } 
     } 

    } 

    public void stopListenLocation(){ 
     //stop updating after retrieving the location 
     locationManager.removeUpdates(this); 
    } 

    public void requestLocationUpdates(){ 
     if(provider != null && !provider.isEmpty()){ 
      locationManager.requestLocationUpdates(provider, 60000, 10, this); 
     } else{ 
      Log.e("MyLocationManager", "Request Location Updates Failed: Provider is null."); 
     } 
    } 


    @Override 
    public void onLocationChanged(Location location) { 
     currentLocation = location; 
     Log.d("SONIC", "location changed to "+location); 

    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 

} 
+0

首先您必須確保GPS已啓用。第二件事是getLastKnownLocation()不能保證它有最後的位置。因此,您需要檢查GPS是否無法提供位置,而不是您可以使用NETWORK提供商獲取位置。 – 2014-10-28 04:56:38

+0

@BirajZalavadia嗨,我確定GPS已啓用。我的網絡定位適用於所有設備,但我只是想確保GPS工作。我只是想知道爲什麼getLastKnownLocation()適用於相同條件下的某些設備。我嘗試了GPS的requestLocationUpdates(),但它也沒有工作。你有這個解決方案嗎?謝謝 – GilbertLee 2014-10-28 05:14:42

回答

0

其實這是一個設備問題。

一些設備不支持getLastKnownLocation(例如Motorola XT720)。您必須執行LocationListener並自行將位置更新保存在onLocationChanged中,並保存到數據庫或sharedpreferences。當getLastKnownLocation爲空時,使用該保存的位置。

你可以用最佳供應辦法去得到快速的位置儘可能地。

this博客希望它可以幫助你。

+0

嗨,我不確定這一點。我可以上傳我的代碼,並且可以爲我檢查嗎? – GilbertLee 2014-10-28 05:28:25

+0

雅爾肯定發佈您的代碼 – 2014-10-28 05:29:09

+0

請檢查代碼 – GilbertLee 2014-10-28 05:34:13