0

사용자가 대략적인 위치를 알고 있어야하는 앱을 만들고 있습니다. 테스트 한 모든 다른 휴대 전화에서 제대로 작동하지만 내 xperia X 성능에서 getLastKnownLocation() 예외없이 항상 null을 반환합니다.locationManager.getLastKnownLocation()은 항상 새로운 xperia X 성능에 null을 반환합니다.

이 코드는 다른 기기에서 작동하며 GPS는 다른 휴대 전화에서 작동합니다.

public boolean startLocationManager() { 
    // Check for permission to use Fine Location 
    if (ContextCompat.checkSelfPermission(mActivity, 
      Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 

     try { 
      locationManager = (LocationManager) mActivity.getSystemService(LOCATION_SERVICE); 

      String providerInfo = null; 

      // Check if Network provider is enabled 
      if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
       providerInfo = LocationManager.NETWORK_PROVIDER; 
      } 
      else{ 
       showSettingsAlert(); 
      } 

      // Application can use GPS or Network Provider 
      if (providerInfo != null) { 
       locationManager.requestLocationUpdates(
         providerInfo, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, 
         this 
       ); 

       if (locationManager != null) { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       } 

       if(location != null){ 
        isEnabled = true; 
       } 
       else{ 
        Toast.makeText(mActivity, R.string.location_is_turned_off, Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.e(TAG, "Can't connect to LocationManager", e); 
     } 
    } 
    return isEnabled; 
} 

I 앱에게 올바른 권한을 부여했고 나는 아직도 getLastKnownLocation()에서 널 (null)하지만 아무것도 얻을 수 없습니다.

이 어떤 도움이 있어요하지만 만약 내가 모르는을 heres 일부 전화 정보 : 코드 아래 Phone info

답변

0

사용. 어떤 경우 이전 위치를 캐시 검색합니다 때문에

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

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

     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network Enabled"); 
       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 

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

    return location; 
} 
+0

'locationManager.getLastKnownLocation (LocationManager.NETWORK_PROVIDER);'은 여전히 ​​null을 반환합니다. – lins

0
if (providerInfo != null) { 
    locationManager.requestLocationUpdates(
     providerInfo, 
     MIN_TIME_BW_UPDATES, 
     MIN_DISTANCE_CHANGE_FOR_UPDATES, 
     this 
    ); 

if (locationManager != null) { 
    location = locationManager.getLastKnownLocation(
     LocationManager.NETWORK_PROVIDER); 
} 

getLastKnownLocation에서 위치를 얻을 보장 아니에요. onLocationChanged 콜백이 실행될 때까지 기다려야합니다.

+0

외부에서 걸어 다니면서 앱을 10 분 동안 실행하고 'onLocationChanged()'가 한 번 실행되지 않았습니다. – lins

+0

minTime 및 minDistance 매개 변수는 어떻게됩니까? 테스트를하려면이 매개 변수에 0을 전달하고 onLocationChanged를 기다려보십시오. 또는 [u] (https://code.google.com/p/android/issues/detail?id=57707)와 같은 것이 있습니다. – Blackkara