• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help with GPS app. No speed value after screen rotation

I'm playing with a GPS application and am having issues when the screen rotates.

The following is what I use to create the location updates and stop them when the screen rotates.

The app, when started shows the speed and location updates, once the screen rotates I no longer have the speed updates, although the location update procedure does kick in as the location changes and I update an elapsed time within it.

Here is the code, have I missed something obvious?

Code:
    @Override    
    public void onResume() { 
        super.onResume();
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if(gps<2){
            try {
                // get location manager to check gps availability
                boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER); 
    
                if(!isGPS){
                    gps++;
                    if(gps<2) checkGps();
                    else finish();
                }
                else{
                    gps=2;                
                    //gps is available, do actions here    
                    
                }                
            
             } catch (Exception e1) {
                 gps++;
                 if(gps<2) checkGps();
                 else finish();
            }
        }
        
        if (listenerFine == null){
            
            listenerFine = null;
        }
            
        
        listenerFine = new LocationListener() {
            
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                speed = location.getSpeed() ;
                
                <[I]Other stuff being updated etc in here[/I]>
                
            }
        };
        

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, listenerFine);

        
        
    }
    
    public void onPause() {
        super.onPause();
        locationManager.removeUpdates(listenerFine);
        
        
    }
 
When the screen is rotated the entire app is killed and restarted from OnCreate, so the problem might not be in your pause/resume procedures.

Yeah, I know the activity is destroyed/recreated on rotation, thus the onResume/onPause portions of the app.
onResume is also called when the activity is created, as the speed works the first time the activity is opened, but not after rotation.

What is weird is that the onLocationChange method is still being called every second as the location and time elapsed, which I calculate in the method, are being updated.
The Speed isn't.

I am missing something obvious and if I could get the DDMS working with the emulator to play GPX files, I could have a nose around while debugging, but it will not recognise any files (GPX and KML) that I have.

Cheers,

Paul.
 
Upvote 0
Can you just step through it in the debugger? Is gps being set correctly before the on resume? I'm not sure why all this is in onresume, it seems that oncreate is a more appropriate place to set up all your listeners and stuff. But I've never made a gps app, so maybe that's the standard.
 
Upvote 0
Hi,

I can test the basic operations of the app, but when running the app on the phone, when it rotates the speed value stops being picked up from the location object.
I would like to be able to test it against a known gps track on the emulator for ease of debugging and be able test further functions.
There are a couple of order small niggles that is like to iron out.

Cheers for the advice, but I'm going to get the gps tracks running even if it kills me. :)
Paul
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones