2013-07-20 31 views
1

我使用三星手機通過LocationManager API獲取位置,如果GPS禁用,我無法獲取位置,通過網絡提供商我無法獲取位置。如果GPS被禁用,位置沒有進入三星手機

這裏是代碼,這在HTC & sony甚至停用GPS,但不在三星手機。

public Location getLocation() { 
     try { 
      mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = mLocationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = mLocationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); //fails in samsung phone returns NULL 

      if (isGPSEnabled == false && isNetworkEnabled == false) { 
       // no network provider is enabled 
      } else { 
       this.canGetLocation = true; 
       if (isNetworkEnabled) { 
        mLocationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        if (mLocationManager != null) { 
         mLocation = mLocationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (mLocation == null) { 
         mLocationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         if (mLocationManager != null) { 
          mLocation = mLocationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return mLocation; 
    } 
+0

之前運行下面的代碼啓用你的網絡供應商? – Rajeev

+0

嗨,是它啓用 – Naruto

回答

1

此行爲是三星手機所特有的。宏達電你不會遇到這個問題。

您必須啓動locationManager,因爲默認情況下它會查看Google地圖緩存並且不關心當前位置信息緩存的陳舊程度。做到這一點,啓動您的應用程序,形成一個不同的GPS位置,並注意它仍然顯示舊的位置。現在啓動Google地圖,然後重新啓動您的應用,您的位置就會發生變化。要以編程方式啓動您的「當前位置」的應用程序調用getLastKnownLocation

YOUR_APPLICATION_CONTEXT.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
    @Override 
    public void onProviderEnabled(String provider) { 
    } 
    @Override 
    public void onProviderDisabled(String provider) { 
    } 
    @Override 
    public void onLocationChanged(final Location location) { 
    } 
});