Here is a code below that suppose to listen for GPS updates and send notifications to main activity
However it does not seem to be working 🙂
No errors, only onLocationChanged is not called!
Funny thing is: when I run some other app to get GPS fix and after that start my app => onLocationChanged gets called, only accuracy is degrading, and eventually fix is lost.
I also tried to add to this code GPS listener=> it works, satellites in view got reported every second, only fix is never accuired.
What the hell is going on? 🙂
public class PhoneGPSpos{
private Handler myHandler = null;
private LocationManager lm;
private LocationListener locationListener;
private Location currentBest;
Context m_context;
public PhoneGPSpos(Context context, Handler handler) {
myHandler = handler;
m_context = context;
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// Bundle bundle = new Bundle();
// boolean xtraInjection=lm.sendExtraCommand(LocationManager.GPS_PROVIDER,"force_xtra_injection",bundle);
// boolean timeInjection=lm.sendExtraCommand(LocationManager.GPS_PROVIDER,"force_time_injection",bundle);
currentBest = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
Log.w("Compass", "Loc");
if (location != null)
{
currentBest = location;
Message msg = new Message();
msg.arg1 = 5;
myHandler.sendMessage(msg);
}
}
public void onStatusChanged(String provider, int status, Bundle
extras) {
Log.d("Compass", "Stat");
}
public void onProviderEnabled(String provider) {
Log.d("Compass", "Prov e");
}
public void onProviderDisabled(String provider) {
Log.d("Compass", "Prov d");
}
};
}
public void requestGPS()
{
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Log.d("Compass", "gps requested");
}
public void stopGPS()
{
Log.d("Compass", "gps stopped");
lm.removeUpdates(locationListener);
}
public synchronized Location getBest()
{
return currentBest;
}
}
Ok, figure out that this is an issue that was also seen by other people: apparently (at least on some phones) GPS listener works much worse if Camera is active.
Problem with getting GPS data when using CameraPreview
Android: Losing GPS signal strength on some phones when I start up the Camera and SurfaceView
Camera Preview and GPS reading in Android
Created Andorid issue for that http://code.google.com/p/android/issues/detail?id=20098
If someone has a solution pls share