3

After I closed my app for a while then reopen it again,my app will not update location or sometime it will take long time( about 5min) before update. How can I fix it? This is my code

  private LocationManager lm;
private LocationListener locationListener;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    

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

    locationListener = new mLocationListener();

    lm.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 0, 0, locationListener); }


private class myLocationListener implements LocationListener 
{
    @Override
    public void onLocationChanged(Location loc) {
        if (loc != null) {
            TextView gpsloc = (TextView) findViewById(R.id.widget28);
            gpsloc.setText("Lat:"+loc.getLatitude()+" Lng:"+ loc.getLongitude());
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        TextView gpsloc = (TextView) findViewById(R.id.widget28);
        gpsloc.setText("GPS OFFLINE.");
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, 
        Bundle extras) {
        // TODO Auto-generated method stub
    }
}
mrmamon
  • 151
  • 1
  • 3
  • 7
  • 9
    One thing I noticed: you should really unregister your location listener in onPause(). Otherwise it will keep getting updates and the GPS will keep running for no reason (not good). That being said, register it in onResume(), not onCreate(). – mxk Sep 07 '09 at 15:33
  • 2
    You also don't need a separate class for it. You can just have your activity implement the LocationListener interface and then override the methods there. – MattC Sep 07 '09 at 20:38
  • Check Blow Link.. May be it's help you... In that u have to pass sach argument so that Location manager can easily Update Your Location and your code is also right to get Location .... http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28long,%20float,%20android.location.Criteria,%20android.app.PendingIntent%29 – Mitesh Jain Aug 03 '11 at 12:06

2 Answers2

8

A few things...

What's probably happening is your app is being resumed and not created the "second" time. If you want it to look for a new location every time the activity is viewed you'll want to move the following code into the onResume() method:

lm.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 0, 0, locationListener); }

You'll also want to make sure you unregister your LocationListener in the onPause() method. Something like this should do it:

@Override
public void onPause() {
    lm.removeUpdates(locationListener);
    super.onPause();
}

It looks like you have a typo on this line:

locationListener = new mLocationListener();

I think it should be:

locationListener = new myLocationListener();

Also, as someone else mentioned, there's no reason to create a whole new class to act as your LocationListener. In fact, it'll burn up memory unnecessarily and on mobile devices memory is at a premium.

The combination of all of the things above would leave your Activity looking something like this:

public class Whatever extends Activity implements LocationListener {
    private LocationManager lm;
    private LocationListener locationListener;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);    

        lm = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);    
        locationListener = new mLocationListener();
    }

    @Override 
    public void onResume() {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
        super.onResume();
    }

    @Override
    public void onPause() {
        lm.removeUpdates(this);
        super.onPause();
    }

    @Override
    public void onLocationChanged(Location loc) {
        if (loc != null) {
            TextView gpsloc = (TextView) findViewById(R.id.widget28);
            gpsloc.setText("Lat:"+loc.getLatitude()+" Lng:"+ loc.getLongitude());
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        TextView gpsloc = (TextView) findViewById(R.id.widget28);
        gpsloc.setText("GPS OFFLINE.");
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
}
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
0

first thing why u have used 0 mintime and 0 precision it will drain your battery very fast..avoid this if its not necessity
secondly the time lag u mentioned here is definitely the time taken by the gps chip to access the gps data which can vary sometimes depending on GPS signal availability
and regarding update u should try this

public void onLocationChanged(Location loc) {
        if (loc != null) {
            TextView gpsloc = (TextView) findViewById(R.id.widget28);
            gpsloc.setText("Lat:"+loc.getLatitude()+" Lng:"+ loc.getLongitude());
        }
        else
             {gpsloc.setText("provider not available");}//provider available or not 

    }

there might be a chance that there is no provider available to update the location

aps109
  • 167
  • 1
  • 3
  • 15