2012-06-21 38 views
0

我是新手。我写了一个类,实现服务和使用GPS,但位置总是空。所有需要的权限被写入,服务通常开始,我可以绑定到它,但检索到的位置是空的,它永远不会在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

当然,我已经尝试过使用DDMS,带有地理修复命令的telnet和谷歌这个问题 - 并找不到解决方案,直到阅读下面的Yury评论。 – v1pka

回答

0

如果您在2.3版本的Android中使用模拟器,那么就有一个bug。我最近才面对它。在这种情况下,您可以安装2.3 x86模拟器。它工作正常。

+0

太棒了!谢谢你的帮助! – v1pka