1

I have problem with gps implementation on Android. The problem is GPS lose fix. The location icon is pulsing, after few seconds toast shows with position and then GPS lose fix and try to connect again.

My service:

public class LocationService extends Service {

    private static final long MIN_TIME = 15000;
    private static final float MIN_DISTANCE = 1;

    private final IBinder binder = new LocalBinder();

    private LocationManager locationManager;

    @Override
    public void onCreate() {
        super.onCreate();

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);     
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public class LocalBinder extends Binder {
        LocationService getService() {
            return LocationService.this;
        }
    }

    public void setListener(LocationListener listener) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, listener);
    }

    public void removeListener(LocationListener listener) {
        locationManager.removeUpdates(listener);
    }
}

My activity:

public class MainMapActivity extends CustomMapActivity implements LocationListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);

        Intent i = new Intent(this, LocationService.class);
        startService(i);
        bindService(i, conn, BIND_AUTO_CREATE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

    ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            serv = ((LocalBinder)service).getService();
            serv.setListener(MainMapActivity.this);
        }
    };

    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(this, Double.toString(location.getLatitude())+Double.toString(location.getLongitude()),     Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}

It is not a problem with device, because in Google Map App it works perfect.

Thanks for help

4

1 回答 1

1

You have passed a minimum time parameter of 15 seconds to the location listener. In most devices/OS versions this will turn off the GPS for that amount of time before re-enabling it.

于 2012-08-18T16:08:48.197 回答