2013-09-24 2 views
0

이 코드 : LocationManager, GPS 및 네트워크 제공 업체가 모두 트리거됩니까?

ServiceListener mlistener = new SosServiceListener(getApplicationContext()); 

manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

manager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mlistener, null); 
manager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, mlistener, null); 

이 두 이벤트에 한 번 수신기를 연결합니다. 사실 : 두 이벤트가 모두 트리거되면 리스너가 두 번 호출됩니까? 그렇다면, 어떻게 예방 하시겠습니까?

감사합니다. 이 장치는 구글 장치에서 실행된다는 것을 가정 할 경우

+0

해당 목적으로 만 - else 루프가 생성 된 경우 ... – WISHY

+0

네트워크 공급자 및 GPS 공급자를위한 두 개의 diff 수신기 하나를 만들고 두 결과를 모두 읽습니다. 귀하의 관심에 따라 귀하는 GPS 또는 네트워크 제공 업체 중 하나를 취소하거나 중지 할 수 있습니다. 대답 : – user755

답변

1

이 내 프로젝트 중 하나에 사용하고 완벽하게 작동하고 무엇을 :

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

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

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

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
      } else { 
       // First get location from Network Provider 
       if (isNetworkEnabled) { 
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (mLocationManager != null) { 
         location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          lat = location.getLatitude(); 
          lng = location.getLongitude(); 
         } 
        } 
       } 
       //get the location by gps 
       if (isGPSEnabled) { 
        if (location == null) { 
         mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (mLocationManager != null) {location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           lat = location.getLatitude(); 
           lng = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

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

     return location; 
    } 
+0

대답 주셔서 감사합니다 :) – Filnik

+0

어딘가에 정의 된 이러한 상수 또는 내가 볼 때 내 값을 설정할 수 있습니까? 그렇다면 어떤 값을 할당해야합니까? – Libathos

+0

여기에 상수가 없습니다. lat와 lng는 이중 변수이고 isGPSEnabled 및 isNetworkEnabled는 부울 변수이며 false로 설정되어 있습니다. mLocationManager는 LocationManager의 인스턴스입니다. – TharakaNirmana

관련 문제