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
traigo
Posts: 79
Registered: ‎11-10-2009
My Device: Torch 9800
Accepted Solution

GPS Polling appears to be blocking on setLocationListener

I am trying to start GPS polling, but setLocationListener appears to be blocking.  The application just hangs there.  This is on a Torch 9810 device.  I'm using SDK 6.  I'm not getting any errors, it just appears to stop.  After MUCH research, I came up with this:

I have static provider and criteria variables in my main application class.

To start polling, I call setupGpsPolling();

I set a location listener with my interval.

I have a thread that checks for the last fix.  If the time is greater than 20 minutes, it resets by calling setupGpsPolling();

 

In my main application class::

	private static BlackBerryLocationProvider _provider = null;
	private static BlackBerryCriteria _criteria;
......


	public static void setupGpsPolling() {
		LocationInfo.setLocationOn(); 
		if (_provider != null) {
			_provider.setLocationListener(null, 0, 0, 0);
			_provider.reset();
			_provider = null;
		}

		try {
			_criteria = new BlackBerryCriteria();
			_criteria.setGPSRestartInterval(600, 0);
			_criteria.setSpeedAndCourseRequired(true);
			_provider = (BlackBerryLocationProvider) LocationProvider.getInstance(_criteria);
			_provider.setLocationListener(new LocationListener(), 300, -1, 0);
		} catch (LocationException e) {
			System.out.println(e.getMessage());
		}
	}

 

 

 LocationListener

public class LocationListener implements javax.microedition.location.LocationListener{

	private static long lastFix = 0l;
	private static Object lock = new Object();
	
	public void locationUpdated(LocationProvider provider, Location location) {
		try {
			BlackBerryLocation loc = (BlackBerryLocation) location;
	    	double longitude = location.getQualifiedCoordinates().getLongitude();
	    	double latitude =location.getQualifiedCoordinates().getLatitude();
	    	float speed = location.getSpeed();
	    	float course = location.getCourse();
	    	setLastFix(location.getTimestamp());
	    	GPSLocation gps = new GPSLocation(longitude, latitude, speed, course, location.getTimestamp());
	    	gps.setStatus(loc.getStatus());
	    	gps.setError(loc.getError());
	    	gps.process();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void providerStateChanged(LocationProvider provider, int newState) {
		
	}

	public static void setLastFix(long lastFix) {
		synchronized (lock) {
			LocationListener.lastFix = lastFix;
		}
	}

	public static long getLastFix() {
		synchronized (lock) {
			return lastFix;
		}
	}

}

 

My checker thread:

public class GPSThread extends Thread {
	
	public GPSThread() {
	}
	
	public void run() {
		while (true) {
			try {
				Thread.sleep(300000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if ((System.currentTimeMillis() - LocationListener.getLastFix()) > (20 * 60 * 1000)) {
				App.setupGpsPolling(); //reset our location listener
			}
		}

	}
}

 

Developer
traigo
Posts: 79
Registered: ‎11-10-2009
My Device: Torch 9800

Re: GPS Polling appears to be blocking on setLocationListener

_provider.setLocationListener(new LocationListener(), 300, -1, 0)

Should have been

 

_provider.setLocationListener(new LocationListener(), 300, -1, -1)
Regular Contributor
walkin
Posts: 52
Registered: ‎11-24-2011
My Device: Bold 9930

Re: GPS Polling appears to be blocking on setLocationListener

[ Edited ]

I also have this issue with:

_locProvider.setLocationListener(null, 0, -1, -1);
_locProvider.reset();

If I comment first line, I'm stack on the reset() !?

Interesting thing is that this works on real devices and on Simulator 9930 - default for OS7.0,

and does not on Simulator 9800 - default for OS6.0

More to it, in the code I also use listener like:

_locProvider.setLocationListener(this, interval, -1, -1);

and it works OK in all cases!?

In fact, I call it before and later call it with null to cancel listening.

Yes, I exited locationUpdated() before resetting listener.

Setting breakpoint on the first line and then continue shows that

some BlackBerryLocationProvider.BlackBerryListenerThread thread was stopped.

 

I'm using Eclipse plug-in 1.5.2 on Win7/64.

....

P.S. I read all (big) posts here and also tried:

_locProvider.setLocationListener(null, 0, 0, 0);
_locProvider.reset();

 with the same result.

 

Regular Contributor
walkin
Posts: 52
Registered: ‎11-24-2011
My Device: Bold 9930

Re: GPS Polling appears to be blocking on setLocationListener

Just a quick question-update.

How about that the problem with setLocationListener(null,...) (or reset()) happens only when it is called before(!)

the very first locationUpdated() was called?

I mean, in case getLastKnownLocation() returns null?