How to release GPS Resource

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

How to release GPS Resource

Postby kristofer » Fri Oct 08, 2010 12:46 pm

Hi there,I have a little map app that only shows my current location and another point using Google MapActivity. If I leave my app, the gps sensor seems not be released, so after an hour my battery is empty regardless how long I really used the app.
Code: Select all
public class GoogleMapsHandler extends MapActivity implements NavigationHandler {

   private MapController mapController;

   private GoogleMapsUpdateHandler updateHandler;

   private MapView mapView;

   private GoogleMapsTargetOverlay targetOverlay;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
      Boolean emumode = preferences.getBoolean("emumode", false);
      if (emumode) {
         setContentView(R.layout.mapemu);
      } else {
         setContentView(R.layout.map);
      }

      this.mapView = (MapView) findViewById(R.id.mapview);
      Long id = (Long) getIntent().getExtras().get("object_id");
      Long xcoord = (Long) getIntent().getExtras().get("xcoord");
      Long ycoord = (Long) getIntent().getExtras().get("ycoord");

      SQLiteDao dao = new SQLiteDao(this);
      Stop stop = dao.getStopById(id);
      dao.close();
      this.mapController = this.mapView.getController();
      this.mapController.setZoom(17);
      this.mapController.setCenter(new GeoPoint(ycoord.intValue(), xcoord.intValue()));

      this.mapView.setBuiltInZoomControls(true);

      this.updateHandler = new GoogleMapsUpdateHandler(this, this.mapView, this.mapController);
      this.targetOverlay = new GoogleMapsTargetOverlay(this, stop, ycoord.intValue(), xcoord.intValue());
      this.mapView.getOverlays().add(this.targetOverlay);
      this.mapView.getOverlays().add(this.updateHandler);
      this.updateHandler.enableCompass();
      this.updateHandler.enableMyLocation();
      this.updateHandler.runOnFirstFix(new Runnable() {

         @Override
         public void run() {
            GoogleMapsHandler.this.mapController.animateTo(GoogleMapsHandler.this.updateHandler.getMyLocation());
         }
      });
   }

   @Override
   public void onPause() {
      Log.v("debug", "paused the update");
      this.updateHandler.disableCompass();
      this.updateHandler.disableMyLocation();
      super.onPause();
   }

   @Override
   protected void onResume() {
      Log.v("debug", "resumed the update");
      this.updateHandler.enableCompass();
      this.updateHandler.enableMyLocation();

      super.onResume();
   }

   @Override
   protected boolean isRouteDisplayed() {
      return false;
   }
}

public class GoogleMapsTargetOverlay extends ItemizedOverlay {

   private static Drawable DEFAULT_MARKER;

   private OverlayItem item;

   private Context context;

   private static Drawable getMarker(Context context) {
      if (GoogleMapsTargetOverlay.DEFAULT_MARKER == null) {
         GoogleMapsTargetOverlay.DEFAULT_MARKER = context.getResources().getDrawable(R.drawable.position);
      }
      return GoogleMapsTargetOverlay.DEFAULT_MARKER;
   }

   public GoogleMapsTargetOverlay(Context context, Stop stop, int latE6, int lonE6) {
      super(getMarker(context));
      this.context = context;
      this.item = new OverlayItem(new GeoPoint(latE6, lonE6), stop.getTitle(), stop.getTitle());

      populate();
   }

   @Override
   protected boolean onTap(int i) {
      Toast.makeText(this.context, this.item.getSnippet(), Toast.LENGTH_SHORT).show();

      return (true);
   }

   @Override
   public void draw(Canvas canvas, MapView mapView, boolean shadow) {
      super.draw(canvas, mapView, shadow);

      boundCenterBottom(GoogleMapsTargetOverlay.DEFAULT_MARKER);
   }

   @Override
   protected OverlayItem createItem(int i) {
      return this.item;
   }

   @Override
   public int size() {
      return 1;
   }

}

public class GoogleMapsUpdateHandler extends MyLocationOverlay {

   private MapController mapController;

   public GoogleMapsUpdateHandler(Context context, MapView mapView, MapController mapController) {
      super(context, mapView);
      this.mapController = mapController;
   }

   @Override
   public void onLocationChanged(Location location) {
      int latE6 = (int) (location.getLatitude() * 1E6);
      int lonE6 = (int) (location.getLongitude() * 1E6);
      this.mapController.animateTo(new GeoPoint(latE6, lonE6));
   }

   @Override
   public void onProviderDisabled(String provider) {

   }

   @Override
   public void onProviderEnabled(String provider) {

   }

   @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {

   }

}


Any comments are appreciated, Thanks in advance
kristofer
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Oct 08, 2010 12:05 pm

Top

Re: How to release GPS Resource

Postby jorn88 » Wed Oct 20, 2010 10:27 pm

Hey there!

In order to deactive the GPS tower when your app closes, add in this:

@Override
public void onDestroy() {
super.onDestroy();
myLocationManager.removeUpdates(myLocationListener);
}

Hope this helps!
jorn88
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 20, 2010 10:23 pm

Re: How to release GPS Resource

Postby blundell » Thu Oct 21, 2010 10:16 am

Yes as jorn says, but your best off releaseing the resource in onPause and checking then asking for it again in onResume. GL
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Map Problems

Who is online

Users browsing this forum: Exabot [Bot], Majestic-12 [Bot] and 4 guests