public void onPause() { if (sensorAttached) { manageUpdateListener(false, updateListener); sensorHelper.detach(); sensorAttached = false; } }
protected void manageUpdateListener(boolean register, SensorEventListener listener) { if (register) { if (!listeningForUpdate) { sensorAttached = sensorHelper.attach(proxy.getTiContext().getActivity()); if (sensorAttached) { LocationManager locationManager = (LocationManager) proxy.getTiContext().getActivity().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locationManager.getBestProvider(criteria, true); if (provider != null) { Location location = locationManager.getLastKnownLocation(provider); if (location != null) { geomagneticField = new GeomagneticField( (float) location.getLatitude(), (float) location.getLongitude(), (float) location.getAltitude(), System.currentTimeMillis()); } } sensorHelper.registerListener(SENSORS, listener, SensorManager.SENSOR_DELAY_UI); listeningForUpdate = true; } } } else { if (listeningForUpdate) { sensorHelper.unregisterListener(SENSORS, listener); if (sensorHelper.isEmpty()) { listeningForUpdate = false; sensorHelper.detach(); } } } }