tags:

views:

1120

answers:

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
    }
}
+3  A: 

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
    }
}
fiXedd
I don't understand what "locationListener = new mLocationListener();" does in your code.Could you explain?
systempuntoout
It looks like it was a typo. Just remove the 'm' from the beginning.
fiXedd
A: 

I'm using similar code . How to alert user(toast message or progress dialog ) on starting application during the GPS is initializing ?

A: 

@fiXedd Need I use OnStart() method or only OnPause() is enough?