onLocationChanged() is never trigered by requestLocationUpdate() - Android

廉价感情. 提交于 2019-11-28 12:55:48

问题


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?

Code:

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

Output:


回答1:


This is a duplicate of this question, also asked by you. In any case, you could try using GoogleAPIClient - like this:

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

Where the googleApiClient is instantiated and connected in some helper method like this:

/**
     * 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();
    }

You also need to make sure you have added the permission:

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

Your problem might be related to the one discussed here - so please also look at the selected answer there and see if it does not help your case as well.




回答2:


This is what a typical location request should look like:

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);
        }
    });
}

EDIT: like @ishmaelMakitla said,

make sure to have the following permission:

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


来源:https://stackoverflow.com/questions/37103136/onlocationchanged-is-never-trigered-by-requestlocationupdate-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!