requestLocationUpdate()永远不会触发onLocationChanged()-Android [英] onLocationChanged() is never trigered by requestLocationUpdate() - Android

查看:786
本文介绍了requestLocationUpdate()永远不会触发onLocationChanged()-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用manager.requestLocationUpdates("gps", 1000, 0, new LocationDetector());每秒获取更新.但是此方法永远不会触发onLocationChanged()方法.我已经运行了很多次应用程序,并且还尝试等待30分钟以获取更新,但是Android Monitor上没有显示更新.我还仔细检查了设备的GPS处于打开状态.因此,请告诉我们可能是什么问题?

I have used manager.requestLocationUpdates("gps", 1000, 0, new LocationDetector()); to get update after every second. But this method is never triggering onLocationChanged() method. I have run my application many times, and I have also tried to wait up to 30 minutes for getting update, but no update is being displayed on Android Monitor. I have also double checked my device's GPS which is ON. So Kindly tell what can be the problem?

代码:

public class LocationDetector implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        Log.d("GPS: ", location.getLatitude()+ ", " +location.getLongitude());
    }

输出:

推荐答案

这是典型的位置请求的外观:

This is what a typical location request should look like:

注意:我将Activity作为参数,因为我是从Activity调用它的.它在我的util类中.

Note: I have Activity as a parameter because I call this from an Activity. It is in my util class.

public static void getLocation(Activity activity) {
    final LocationListener locationListener = new LocationListener() {
        @Override
        public void onProviderEnabled(String provider) {
            Log.i(TAG, "onProviderEnabled");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.i(TAG, "onProviderDisabled");
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.i(TAG, "onLocationChanged");
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.i(TAG, "onStatusChanged");
        }
    };

    final LocationManager locationManager = (LocationManager) appContext.getSystemService(Context.LOCATION_SERVICE);
    final Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setCostAllowed(false);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(false);

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            locationManager.requestLocationUpdates(1000, 0, criteria, locationListener, null);
        }
    });
}

就像@ishmaelMakitla说的那样,

like @ishmaelMakitla said,

确保具有以下权限:

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

这篇关于requestLocationUpdate()永远不会触发onLocationChanged()-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆