GPS does not update location after closing and reopens app on Android

After I have closed my app for a while, rebuild it again, my app will not update the location or someday it will take a long time (about 5 minutes) to update. How can I fix this? 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
    }
}

      

+2


source to share


2 answers


A few things...

What is probably happening is that your application is being resumed and no "second" time is created. If you want it to look for a new location every time you view an activity, you want to move the following code into a method onResume()

:

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

      

You will also want you to unregister your LocationListener in the method onPause()

. 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 mentioned, there is no reason to create a whole new class that will act as your locator. In fact, it will burn memory unnecessarily, and there is a premium on mobile memory.

Combining all of the above will result in 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
    }
}

      

+7


source


first thing why you used 0 mintime and precision 0, it will drain your battery very much. secondly, the time lag mentioned here certainly takes the time taken by the gps chip to access the gps data which can sometimes change depending on the availability of the GPS signal

and regarding the 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 may be a possibility that the provider will not be able to update the location

0


source







All Articles