• 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

MrRalphMan

Newbie
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.
 
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.
 
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.
 
Hiya,

Thanks for the reply, I would go through it with the debugger, but cannot get a test god feed running, either through a nmea file is the test provider on the device, nor through playing a gpx/kml file through ddms.

That's my next task so I cab see what's going on.

Cheers,

Paul
 
If all you want to do is check the operation of the app, why do you have to feed it a gps coordinate file? Can't you just use wherever your emulator thinks it is, or even use your own phone to debug?
 
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
 
Back
Top Bottom