2015-07-10 109 views
1

在我的應用程序中,我嘗試使用networkProvider搜索android設備的粗略位置。我只在我的位置管理器中使用networkProvider,但如果我不打開GPS傳感器,它將不起作用。Android GPS網絡提供者不工作

無論GPS傳感器開啓還是關閉,網絡提供者是否應該給出一個粗略的位置?

這是我的代碼。

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 


     if(isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
      Log.e("Network","suc"); 
     else 
      Log.e("Network", "fail"); 

     if (!isNetworkEnabled) { 

     } else { 
      this.isGetLocation = true; 

      if (isNetworkEnabled) { 
       Log.e("GpsInfo", "isNetworkEnabled true"); 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

       if (locationManager != null) { 
        Log.e("GpsInfoClass", "Location manager not NULL"); 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
                lat = location.getLatitude(); 
         lon = location.getLongitude(); 
         Log.e("GpsInfoClass", lon + ", " + lat); 
        }else{ 
         Log.e("GpsInfoClass", "Location NULL"); 
        } 
       }else{ 
        Log.e("GpsInfo", "Location Manager is null"); 
       } 
      } 
     } 

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

在我的日誌中,它打印網絡(標記)失敗(日誌)。

這是什麼意思是說,

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 

總是返回false,如果GPS傳感器處於關閉狀態。這是爲什麼發生?我錯過了如何獲取粗略位置?

回答

1

GPS傳感器只需要準確的位置。如果您創建了Criteria並將其提供給locationManager,系統將自動選擇最佳提供者並嘗試獲取座標。

示例;

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
Criteria locationMode = new Criteria(); 
locationMode.setBearingRequired(false); 
locationMode.setSpeedRequired(true); 
locationMode.setAccuracy(Criteria.ACCURACY_MEDIUM); 
locationManager.requestSingleUpdate(locationMode, new LocationListener() {@Override 
    public void onLocationChanged(Location location) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
}, null); 
} 
1

是對networkprovider應該給粗略位置,無論GPS傳感器是打開還是關閉?

不,GPS無線電與獲取網絡位置無關。

但是,您需要將您的位置設置設置爲Power SavingHigh Accuracy

使用此代碼來顯示當前啓用的供應商舉杯:

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

    Toast.makeText(context, "GPS Enabled: " + isGpsEnabled + " Network Location Enabled: " + isNetworkEnabled, Toast.LENGTH_LONG).show(); 

下面是它顯示了GPS onlyPower Saving

enter image description here

enter image description here

所以,你可以看到這個電話:

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

對於僅限GPS的設置返回false,因爲這會禁用網絡位置。 而且,它對於節電設置返回true。

它也將返回高準確度,因爲這使GPS和網絡位置。