2012-06-21 2 views
0

나는 초보자입니다. 나는 서비스를 구현하고 GPS를 사용하는 클래스를 작성했지만 위치는 항상 null입니다. 필요한 모든 권한이 작성되고 서비스가 정상적으로 시작되며 바인딩 할 수 있지만 검색된 위치는 항상 null이며 결코 onLocationChanged (위치 위치)에 들어 가지 않습니다. 아무도 도와 줄 수 있니?GPS 서비스 - 위치를 검색 할 수 없습니다.

public class GPSService extends Service { 


    private static final String TAG = "GPS_SERVICE"; 
    private LocationManager mLocationManager = null; 
    private static final int LOCATION_INTERVAL = 1000; 
    private static final float LOCATION_DISTANCE = 10f; 
    Location currentLocation; 

    private class LocationListener implements android.location.LocationListener{ 

     Location mLastLocation; 

     public LocationListener(String provider) 
     { 
      Log.e(TAG, "LocationListener " + provider); 
      mLastLocation = new Location(provider); 
     } 

     public void onLocationChanged(Location location) 
     { 
      Log.e(TAG, "onLocationChanged: " + location); 
      if (location != null){ 
       mLastLocation.set(location); 
       currentLocation = mLastLocation;     
      } 

     } 

     public void onProviderDisabled(String provider) 
     { 
      Log.e(TAG, "onProviderDisabled: " + provider);    
     } 

     public void onProviderEnabled(String provider) 
     { 
      Log.e(TAG, "onProviderEnabled: " + provider); 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) 
     { 
      Log.e(TAG, "onStatusChanged: " + provider); 
     } 

    } 


    LocationListener[] mLocationListeners = new LocationListener[] { 
      new LocationListener(LocationManager.GPS_PROVIDER), 
      new LocationListener(LocationManager.NETWORK_PROVIDER) 
    }; 


    private IBinder mBinder = new GPSServiceBinder(); 

    public class GPSServiceBinder extends Binder { 
     public GPSService getServerInstance() { 
      return GPSService.this; 
     } 
    } 

    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
     Log.e(TAG, "onStartCommand"); 
     super.onStartCommand(intent, flags, startId);  
     return START_STICKY; 
    } 

    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 

    int minTime = 6000; 
    float minDistance = 15; 

    public void onCreate() 
    { 
     Log.e(TAG, "onCreate"); 
     initializeLocationManager(); 

     try { 
      mLocationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
        mLocationListeners[1]); 
     } catch (java.lang.SecurityException ex) { 
      Log.i(TAG, "fail to request location update, ignore", ex); 
     } catch (IllegalArgumentException ex) { 
      Log.d(TAG, "network provider does not exist, " + ex.getMessage()); 
     } 
     try { 
      mLocationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
        mLocationListeners[0]); 
     } catch (java.lang.SecurityException ex) { 
      Log.i(TAG, "fail to request location update, ignore", ex); 
     } catch (IllegalArgumentException ex) { 
      Log.d(TAG, "gps provider does not exist " + ex.getMessage()); 
     } 
    } 


    public Location getLocation() 
    { 
     return currentLocation; 
    } 


    public void onDestroy() 
    { 
     Log.e(TAG, "onDestroy"); 
     super.onDestroy(); 
     if (mLocationManager != null) { 
      for (int i = 0; i < mLocationListeners.length; i++) { 
       try { 
        mLocationManager.removeUpdates(mLocationListeners[i]); 
       } catch (Exception ex) { 
        Log.i(TAG, "fail to remove location listners, ignore", ex); 
       } 
      } 
     } 
    } 


    private void initializeLocationManager() { 
     Log.e(TAG, "initializeLocationManager"); 
     if (mLocationManager == null) { 
      mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
     } 
    } 
} 
+0

에뮬레이터 또는 실제 장치에 있습니까? 에뮬레이터에서 DDMS에 들어가서 수동으로 GPS 위치를 설정 했습니까? 실제 장치에서 휴대 전화가 실내에서 GPS 신호를 수신 할 수 있는지 확인하십시오. – azgolfer

+0

StackOverflow에서 유사한 질문을 확인 했습니까? 위대한 답변과 함께 이미 많은 질문이 있습니다. – MKJParekh

+0

물론 아래의 Yury 의견을 읽을 때까지 지오 픽스 명령을 사용하여 DDMS, 텔넷 및 Google에서이 질문을 시도해 보았습니다. 해결책을 찾지 못했습니다. – v1pka

답변

0

Android 2.3 버전의 에뮬레이터를 사용하는 경우 버그가 있습니다. 나는 최근에 그것에 직면했다. 이 경우 2.3 x86 에뮬레이터를 설치할 수 있습니다. 그것은 잘 작동합니다.

+0

좋아요! 당신의 도움을 주셔서 감사합니다! – v1pka

관련 문제