01-26-2012 02:19 PM
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
}
}
}
}
Solved! Go to Solution.
01-26-2012 04:23 PM
_provider.setLocationListener(new LocationListener(), 300, -1, 0)
Should have been
_provider.setLocationListener(new LocationListener(), 300, -1, -1)
06-27-2012 10:38 AM - edited 06-27-2012 11:46 AM
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.BlackBerryListenerThrea
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.
09-10-2012 09:42 AM
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?