开发者

Strange issue in finding GPS

Hai all i want to find current GPS location, i have used the following code.

public class GPSManager
{
    private static final int gpsMinTime = 500;
    private static final int gpsMinDistance = 0;

    private LocationManager locationManager = null;
    private LocationListener locationListener = null;
    private GPSCallback gpsCallback = null;

    public GPSManager()
    {
        locationListener = new LocationListener()
        {
            public void onProviderDisabled(final String p开发者_如何学编程rovider)
            {
            }

            public void onProviderEnabled(final String provider)
            {
            }

            public void onStatusChanged(final String provider, final int status, final Bundle extras)
            {
            }

            public void onLocationChanged(final Location location)
            {
                location.getLatitude();
                location.getLongitude();

                if (location != null && gpsCallback != null)
                {
                    Log.e("if location "+location,"if gpscallback"+gpsCallback);
                    gpsCallback.onGPSUpdate(location);
                    location.getLatitude();
                    location.getLongitude();
                }
                else{
                    Log.e("else location "+location,"else gpscallback"+gpsCallback);
                }
            }
        };
    }

    public void startListening(final Activity activity)
    {
        if (locationManager == null)
        {
            locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        }

        final Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        final String bestProvider = locationManager.getBestProvider(criteria, true);

        if (bestProvider != null && bestProvider.length() > 0)
        {
            locationManager.requestLocationUpdates(bestProvider, GPSManager.gpsMinTime,
                    GPSManager.gpsMinDistance, locationListener);
        }
        else
        {
            final List<String> providers = locationManager.getProviders(true);

            for (final String provider : providers)
            {
                locationManager.requestLocationUpdates(provider, GPSManager.gpsMinTime,
                        GPSManager.gpsMinDistance, locationListener);
            }
        }
    }

    public void stopListening()
    {
        try
        {
            if (locationManager != null && locationListener != null)
            {
                locationManager.removeUpdates(locationListener);
            }

            locationManager = null;
        }
        catch (final Exception ex)
        {

        }
    }

    public void setGPSCallback(final GPSCallback gpsCallback)
    {
        this.gpsCallback = gpsCallback;
    }

    public GPSCallback getGPSCallback()
    {
        return gpsCallback;
    }

But strange issue is it is working only when GPS in mobile is off mode. If GPS in device is in on Mode it is not working.

Can anybody please tell me what is the issue with my code?

Please help me... thanks in advance.


The problem should be the power requirement. You're requesting for everything that matchs the gps provider ( Location fine .... ) but gps is power consuming so try to remove that line or setting the criteria to POWER_HIGH

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜