Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich Class Should I call locationManger.removeUpdates() from?
    primarykey
    data
    text
    <p>So, I have an Android application that is asking for the GPS from the <code>LocationManager</code> class. I have an <code>Activity</code> that wants to use that data, and a custom class that implements <code>LocationListener</code>. I have written some custom methods to return the GPS values to my other class. </p> <p>Currently, I am asking for and releasing the location updates from my <code>Activity</code> class, not my implementation of the <code>LocationListener</code> class... I think this is the right way to go about it, but I just wanted to get any feedback on the life cycle, etc. (the class that implements <code>LocationListener</code> is not an <code>Activity</code>, so I don't think I even could call <code>onPause()</code> etc, ?)</p> <p>This is the <code>Activity</code>:</p> <pre><code>package com.jessescott.sonicity; import android.app.Activity; import android.content.Context; import android.view.View; import android.widget.TextView; import android.location.Location; import android.location.LocationManager; import android.location.LocationListener; public class PlayActivity extends Activity { // GLOBALS private static final String TAG = "SoniCity"; LocationManager locationManager; MyLocationListener locationListener; TextView latitude, longitude; TextView ActualLatitude, ActualLongitude; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.play_layout); // GPS locationListener = new MyLocationListener(this); // TextViews latitude = (TextView) findViewById(R.id.Latitude); longitude = (TextView) findViewById(R.id.Longitude); ActualLatitude = (TextView) findViewById(R.id.ActualLat); ActualLatitude.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.v(TAG, "Asking For New Latitude"); ActualLatitude.setText(locationListener.getCurrentLatitude()); } }); ActualLongitude = (TextView) findViewById(R.id.ActualLon); ActualLongitude.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.v(TAG, "Asking For New Latitude"); ActualLongitude.setText(locationListener.getCurrentLongitude()); } }); } @Override protected void onPause() { super.onPause(); // Stop GPS locationManager.removeUpdates(locationListener); locationManager = null; } @Override protected void onResume() { super.onResume(); locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, locationListener); } @Override public void onDestroy() { super.onDestroy(); } } /* */ </code></pre> <p>... and this is the <code>LocationListener</code> implementation :</p> <pre><code> package com.jessescott.sonicity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.os.Bundle; import android.util.Log; class MyLocationListener implements LocationListener { private static final String TAG = "SoniCity"; float currentLatitude = 0; float currentLongitude = 0; public MyLocationListener(Context context) { super(); } // Define all LocationListener methods public void onLocationChanged(Location location) { currentLatitude = (float)location.getLatitude(); currentLongitude = (float)location.getLongitude(); } public void onProviderDisabled (String provider) { Log.v(TAG, "Provider is " + provider); } public void onProviderEnabled (String provider) { Log.v(TAG, "Provider is " + provider); } public void onStatusChanged (String provider, int status, Bundle extras) { Log.v(TAG, "Status is " + status); } // Custom Methods public String getCurrentLatitude() { String lat = Float.toString(currentLatitude); return lat; } public String getCurrentLongitude() { String lon = Float.toString(currentLongitude); return lon; } } /* */ </code></pre> <p>I guess since I've moved it to a separate class (they used to be the same one), I just want to make sure I'm calling th request/remove in the right place.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload