2013-01-24 32 views
4

由于某种原因,onLocationChanged不刷新,我只做了一次GPS应用,它只在应用启动时运行一次。onLocationChanged只调用一次,不刷新

这里是我的代码: “位置已更改”

public BackgroundLocationService() { 
     super("myintentservice"); 

     locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

     Criteria crt = new Criteria(); 
     crt.setAccuracy(Criteria.ACCURACY_FINE); 
     String bestProvider = locManager.getBestProvider(crt, true); 

     boolean gps_enabled; 
     boolean network_enabled; 
     boolean best_enabled; 

     gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     best_enabled = locManager.isProviderEnabled(bestProvider); 


     if (best_enabled) { 
      locManager.requestLocationUpdates(bestProvider, 15000, 20, locListener); 
      Log.i(TAG, "Best enabled: " + bestProvider); 
     } else { 
      Log.i("Location Provider: ", "best not enabled: " + bestProvider); 
      if (gps_enabled) { 
       locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener); 
       Log.i(TAG, "gps enabled!"); 
      } 
      if (network_enabled) { 
       locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener); 
       Log.i(TAG, "network enabled!"); 
      } 
     } 

    } 



    class MyLocationListener implements LocationListener { 
     public void onLocationChanged(Location location) { 

      if (location != null) { 

       //locManager.removeUpdates(locListener); 
       longitude = Double.toString(location.getLongitude()); 
       latitude = Double.toString(location.getLatitude()); 

       Log.i(TAG,"Location has CHANGED!"); 
      } 
     } 
     public void onProviderDisabled(String arg) {} 
     public void onProviderEnabled(String arg) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    } 

的消息仅在每个应用程序启动时显示一次。 即时消息在办公室,但我可以移动距离我的位置至少5-10米,并且所有提供商都已启用,因此它必须正常,不是吗?

任何想法?

+0

我认为你应该指定较少米:locManager.requestLocationUpdates(bestProvider,15000,5,locListener); (例如) – SolArabehety

回答

3

当您注册位置侦听器时,您正在请求更新,最短时间为15秒,最小距离为20米。由于您位于办公室内部,因此您不可能获得足够精确的GPS定位以实际检测到5-10米的移动。你尝试过外面的调试吗?同样,如果您启用了模拟位置,请将模拟坐标发送到模拟器,以确保您的应用按照预期更新位置时的行为。

+0

你是对的先生。 –

1

尝试 -

locManager.requestLocationUpdates(bestProvider, 0, 0, locListener);