Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW
Accepted Solution

GPS Crash

I am trying to pull GPS coordinates when my app loads.  When I run the code on a simulator and provide the coordinates, it runs just fine.  Its when I run on a physical device that the application basically just freezes.   Below is the code snippet.

 

 

public void run() 
        {
            Criteria cr = new Criteria();
            cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
            cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
            cr.setCostAllowed(true);
            LocationProvider lp;
            try 
            {
                lp = LocationProvider.getInstance(cr);
          	Location l = lp.getLocation(-1);
          	Coordinates c = l.getQualifiedCoordinates();
          	if (c != null) 
          	{
          	    final double longitude;
          	    final double latitude;
          	    longitude = c.getLongitude();
          	    latitude = c.getLatitude();
          	    if (longitude == 0.0 && latitude == 0.0)
          	    {
          	        add(new RichTextField("No GPS Coordinates Available."));
          	    }
          	    else
          	    {
          	        UiApplication.getUiApplication().invokeLater(new Runnable() 
          	        {
          	            public void run() 
          	            {
          	        	pushScreen(new Listings(longitude,latitude));
          	            }
          	         });
          			   	
          	    }   
          	}
          	else
              	{
              		//add(new RichTextField("GPS coordinates not available..."));
              	}
          	} 
          	catch (Exception e) 
          	{
          		String error = e.toString();
                        add(new RichTextField(error));
          	}
           UiApplication.getUiApplication().requestForeground();
        }
    }

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: GPS Crash

This code must be running on the Event Thread, otherwise the

add(new RichTextField(...);

lines would all throw an IllegalStateException.

 

You have to load your screen and put a 'please wait' type screen up while the location is being obtained.

 

You might find this code useful:

http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-1/t...

 

I would especially look at my first comment which provides a very easy to use interface. 

 

In this case, the Runnable would just get the location and would complete when it had it. 

 

Note that there is a chance your code will never get a location and/or will get an invalid location.  You don't check for that.

Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW

Re: GPS Crash

Thanks, I'll give that a try. 

 

Wouldn't the code below be checking if locations are valid or not?

 

if (c != null) 
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: GPS Crash

I think you also need

if ( l.isValid() ) ...

Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW

Re: GPS Crash

Thanks, I'll add that in there. Now that I have it running int he background, i am not crashing.  However it is always timing out with the following error after about 3-5 seconds.  This is with the device outside with a clear view to the sky in every direction. 

 

javax.microedition.location.LocationException: Timed out while waiting for GPS Location

Developer
Posts: 122
Registered: ‎04-29-2009
My Device: 9630, 9700, 9800
My Carrier: Verizon

Re: GPS Crash

With setCostAllowed(true), you're basically forcing assisted mode.  Some carriers (like Verizon) kick back fast with an error under those criteria (as you are seeing with the 3-5 second turn around).

 

For testing, you might try autonomous GPS:

 

setCostAllowed(false), and

setPreferredPowerConsumption(Criteria.POWER_USAGE_MEDIUM)

 

and for getLocation(), I'd give it 100 seconds, that's usually enough.

Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW

Re: GPS Crash

Scott, that did it!  Thanks you 100 times over.  

Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW

Re: GPS Crash

Actually, im getting the timeout error on AT&T's GSM network, but it appears to be working fine on the Verizon network.  Is there a different method needed for each network?

Developer
Posts: 122
Registered: ‎04-29-2009
My Device: 9630, 9700, 9800
My Carrier: Verizon

Re: GPS Crash

I've used the following criteria on GSM (AT&T) and CDMA (Verizon) networks but in a continuous accumulation format (e.g with a LocationListener).

 

                        // Critera for an autonomous fix.
                        autonomousCriteria.setHorizontalAccuracy(1000);
                        autonomousCriteria.setVerticalAccuracy(1000);
                        autonomousCriteria.setCostAllowed(false);
                        autonomousCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_MEDIUM);

 

all other criteria are at their defaults.  I do occasionally have trouble with 9700 hardware.  It seems like if you interrupt the 9700 GPS aquisition at just the wrong time, it can get "stuck" in a partial fix (perhaps interrupting the download of the ephemeris?)  Autonomous GPS fixes with the 9800 seem smooth and consistently fast. (all just my experiences).

Highlighted
Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW

Re: GPS Crash

The only difference I have is my accuracy is set to no requirement.  I am testing with the 9700 on AT&T, so that might be the problem.  I will test on some other hardware and see what the problem might be.