2013-07-03 85 views
0

我一直试图从我的位置管理器中删除位置更新,但即使在removeupdates被调用后,GPS符号也会持续显示。我确保已经传递了正确的侦听器变量名称,并且为侦听器更新执行了代码。我只想找到一次用户的位置。Android - removeUpdates不删除监听器

感谢

public class MapActivity extends FragmentActivity implements OnInfoWindowClickListener, OnCameraChangeListener { 

    Map<Marker, Integer> markers = new HashMap<Marker, Integer>(); 
    VenueManager venueManager; 
    GoogleMap map; 
    long last_map_refresh = 0; 
    Button search_btn; 
    CameraPosition previous_position = null; 
    private LocationManager locationManager; 
    private static final long MIN_TIME = 400; 
    private static final float MIN_DISTANCE = 1000; 
    private ProgressDialog loading; 
    private Button add_venue; 
    SharedPreferences sharedPreferences; 
    private LatLng user_position = null; 
    private NetworkManager network_manager; 
    private Boolean first_map_render = false; 
    private LocationListener location_listener; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map); 

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

     if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(MapActivity.this); 
      builder.setTitle("GPS not enabled"); // GPS not found 
      builder.setMessage("This application requires you to enable GPS location settings. We recommend that you enable 'Use wireless networks' and 'Use GPS satellites'. Would you like to enable this setting now?"); // Want to enable? 
      builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialogInterface, int i) { 
        finish(); 
        MapActivity.this.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
       } 
      }); 
      builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialogInterface, int i) { 
        Intent home_intent = new Intent(MapActivity.this, HomeActivity.class); 
        startActivity(home_intent); 
        Toast.makeText(MapActivity.this, "You will not be able to use the map features of this application until you enable gps settings", Toast.LENGTH_LONG).show(); 
        finish(); 
       } 
      }); 

      builder.create().show(); 
      return; 
     } 
     else 
     { 
      network_manager = new NetworkManager(); 

      location_listener = new LocationListener() { 
       public void onLocationChanged(Location location) { 
        // TODO Auto-generated method stub 

        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 16); 
        map.animateCamera(cameraUpdate); 
        draw_map(new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).build()); 
        user_position = latLng; 

        locationManager.removeUpdates(location_listener); 
      }; 

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, location_listener); 
} 
} 

回答

1

首先使用LocationManager.NETWORK_PROVIDER时,你不应该看到的GPS标志都没有。

我看你使用谷歌地图的Android API v2的,所以我的猜测是,你把这里的代码之外,你有

map.setMyLocationEnabled(true); 

,显示蓝点并不断对其进行更新,同时显示与ActivityMapFragment/MapView

+0

是的,这是正确的我已经设置了此代码之外。这将有助于GPS符号出现吗? – Garbit

+0

@Garbit是的。 'GoogleMap'使用'LoactionClient'在内部使用'LocationManager'与GPS和NETWORK提供者。 –

+0

它是否耗尽电池,我应该在短时间内设置为false? – Garbit