FusedLocationClient不调用onLocationResult [英] FusedLocationClient not calling onLocationResult

查看:94
本文介绍了FusedLocationClient不调用onLocationResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻我正在创建位置客户端,目前我有

I'm creating a location client at the moment, currently i have

 public void startUpdatingLocationProcess(Context context) {

     parentContext = context;

    if (mFusedLocationClient == null){
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(parentContext);

        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (ContextCompat.checkSelfPermission(parentContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, new LocationCallback(){
                @Override
                public void onLocationResult(LocationResult locationResult) {

                    onLocationChanged(locationResult.getLastLocation());
                }

            } , Looper.myLooper());

         }


}

这是我要在整个应用程序生命周期中运行的LocationApi类中,此特定应用程序运行许多活动,因此我不想每次销毁并创建新活动时都重新创建"请求.

This is in a LocationApi class that i want to run throughout the applications lifecycle, this particular app runs many activities so i don't want to "recreate" the request every time i destroy and create a new activity.

允许并检查FINE_LOCATION的权限,logcat中没有错误,并且代码在创建mFusedLocationClient对象时运行requestLocationUpdates方法,但从未调用过"onLocationResult"方法.

The permission for FINE_LOCATION is allowed and checked, there are no errors in the logcat and the code runs the requestLocationUpdates method on creation of the mFusedLocationClient object, but it never calls the "onLocationResult" method.

使用此api是否缺少我的东西?

Is there something i'm missing with using this api?

推荐答案

在请求位置更新之前,您的应用必须连接到位置服务并发出位置请求.像这样的东西:

Before requesting location updates, your app must connect to location services and make a location request. Some thing like this:

private static LocationRequest createLocationRequest() {
    LogHelper.trace("createLocationRequest");
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(200000);
    mLocationRequest.setFastestInterval(300000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    return mLocationRequest;
}

public static void checkLocationService(final Fragment fragment, final FusedLocationProviderClient client, final OnSuccessListener<LocationSettingsResponse> successListener, OnFailureListener failureListener) {

    LogHelper.trace("checkLocationService");
    final LocationRequest request = createLocationRequest();
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(request);

    SettingsClient settingsClient = LocationServices.getSettingsClient(fragment.getActivity());
    Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(builder.build());

    task.addOnSuccessListener(fragment.getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            LogHelper.trace("onSuccess");
            startLocationService(client, request, new LocationCallback());
            successListener.onSuccess(locationSettingsResponse);
        }
    });

    task.addOnFailureListener(fragment.getActivity(), failureListener);
}

这篇关于FusedLocationClient不调用onLocationResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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