Getting location android?

I want to receive location updates every second for a small period of time, so I wrote this code:

     public void registerListener() {

            if (ActivityCompat.checkSelfPermission(c, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(c, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            } else {
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, locationListener);
            }
        }
        private final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {

                 longitude = location.getLongitude();
                 latitude = location.getLatitude();

                Log.v(TAG, ""+longitude + "                       " +latitude);

            }
@Override
        public void onStatusChanged(String provider, int status, Bundle extras) {} 
        @Override
        public void onProviderEnabled(String provider) {} 
        @Override
        public void onProviderDisabled(String provider) {}
    };

I call registerListener() from somewhere else. The problem is, my location update is only called once, and I only get that log once. Also, the log only comes about 5-10 seconds after calling registerListener .

What is the reason of the slowness, and why isn't the location being updated?

Thanks,

Ruchir


You're requesting GPS location. It will take a few seconds to get a satellite lock for the first time. It may never happen if you can't receive a signal lock (indoors in some buildings, too far underground, physical damage to the phone like a loose antenna (common in early gen Samsungs)).

As for not receiving it again- you won't receive it again unless you move at least 10 meters. Change that to 0 and see it come every second.

链接地址: http://www.djcxy.com/p/90666.html

上一篇: 如何使用android AVD安装.apk文件?

下一篇: 获取位置android?