GPS not update location after close and reopen app on android

后端 未结 2 1334
清歌不尽
清歌不尽 2020-12-01 12:03

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

相关标签:
2条回答
  • 2020-12-01 12:07

    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

    0 讨论(0)
  • 2020-12-01 12:23

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题