开发者

Android GPS. Location Listener does not want to acquire a fix :(

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


Check for permissions in your projects AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>


Where are you testing? Indoors? What providers are the other apps using? Network Provider?

My guess is that the apps that are working use Network provider and since you are getting the last known, it will return that location from the previous app location ping. The Network provider helps allocate gps satellites by giving a general location. So when you start app (after running other apps), you have satellites, but (since you are probably indoors) without the network you may lose satellites little by little, decreasing accuracy.

moral of the story: Use the network provider as well, or make sure you have clear view of the sky to test gps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜