Help with GPS app. No speed value after screen rotation


  1. MrRalphMan

    MrRalphMan Member

    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 (Text):
    1.     @Override    
    2.     public void onResume() {
    3.         super.onResume();
    4.         locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    5.         if(gps<2){
    6.             try {
    7.                 // get location manager to check gps availability
    8.                 boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);
    9.    
    10.                 if(!isGPS){
    11.                     gps++;
    12.                     if(gps<2) checkGps();
    13.                     else finish();
    14.                 }
    15.                 else{
    16.                     gps=2;                
    17.                     //gps is available, do actions here    
    18.                    
    19.                 }                
    20.            
    21.              } catch (Exception e1) {
    22.                  gps++;
    23.                  if(gps<2) checkGps();
    24.                  else finish();
    25.             }
    26.         }
    27.        
    28.         if (listenerFine == null){
    29.            
    30.             listenerFine = null;
    31.         }
    32.            
    33.        
    34.         listenerFine = new LocationListener() {
    35.            
    36.             @Override
    37.             public void onStatusChanged(String provider, int status, Bundle extras) {
    38.                 // TODO Auto-generated method stub
    39.                
    40.             }
    41.            
    42.             @Override
    43.             public void onProviderEnabled(String provider) {
    44.                 // TODO Auto-generated method stub
    45.                
    46.             }
    47.            
    48.             @Override
    49.             public void onProviderDisabled(String provider) {
    50.                 // TODO Auto-generated method stub
    51.                
    52.             }
    53.            
    54.             @Override
    55.             public void onLocationChanged(Location location) {
    56.                 // TODO Auto-generated method stub
    57.                 speed = location.getSpeed() ;
    58.                
    59.                 <[I]Other stuff being updated etc in here[/I]>
    60.                
    61.             }
    62.         };
    63.        
    64.  
    65.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, listenerFine);
    66.  
    67.        
    68.        
    69.     }
    70.    
    71.     public void onPause() {
    72.         super.onPause();
    73.         locationManager.removeUpdates(listenerFine);
    74.        
    75.        
    76.     }

    Advertisement
    :
  2. cp1

    cp1 Well-Known Member

    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.
  3. MrRalphMan

    MrRalphMan Member

    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.
  4. cp1

    cp1 Well-Known Member

    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.
  5. MrRalphMan

    MrRalphMan Member

    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
  6. cp1

    cp1 Well-Known Member

    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?
  7. MrRalphMan

    MrRalphMan Member

    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
Loading...

Share This Page