onLocationChanged()永远不会被requestLocationUpdate() - Android所牵制

时间:2016-05-08 18:07:03

标签: android android-gps

我每隔一秒就使用manager.requestLocationUpdates("gps", 1000, 0, new LocationDetector());来获取更新。但是这种方法永远不会触发onLocationChanged()方法。我已多次运行我的应用程序,并且我还尝试等待最多30分钟来获取更新,但Android Monitor上没有显示更新。我还仔细检查了我的设备的GPS是否打开。那么请告诉我们可能出现什么问题?

代码:

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

输出: enter image description here

2 个答案:

答案 0 :(得分:0)

这是this question的副本,也是您提出的问题。无论如何,您可以尝试使用GoogleAPIClient - 就像这样:

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(60000); // update every (1) minutes               
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,locationRequest, yourLocationDetector );

googleApiClient实例化并以某种辅助方法连接:

/**
     * Helper method to connect the Google API Client
     */
    private void connectGoogleAPIClient() {
      googleApiClient = new GoogleApiClient.Builder(context)
                                .addApi(LocationServices.API)
                                .addConnectionCallbacks(this)
                                .addOnConnectionFailedListener(this)
                                .build();
      googleApiClient.connect();
    }

您还需要确保添加了权限:

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

您的问题可能与one discussed here有关 - 所以请在那里查看所选答案,看看它是否对您的案例没有帮助。

答案 1 :(得分:0)

这是典型的位置请求应该是这样的:

注意:我有Activity作为参数,因为我从一个Activity中调用它。它在我的util类中。

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说,

确保拥有以下权限:

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