简体   繁体   English

为什么 LocationCallback 没有在 CallbackToFutureAdapter 中触发?

[英]Why is LocationCallback not firing within CallbackToFutureAdapter?

What I need to accomplish: My application should request the current location of the user, repeating every 15 minutes, also in the background and even when the application is terminated.我需要完成的工作:我的应用程序应该请求用户的当前位置,每 15 分钟重复一次,在后台甚至在应用程序终止时也是如此。

What I did: I am using the FusedLocationProviderAPI, called within a periodical WorkManager, extending the ListenableWorker, as the API is asynchronous.我所做的:我正在使用 FusedLocationProviderAPI,在定期 WorkManager 中调用,扩展 ListenableWorker,因为 API 是异步的。 Each time a new Location Update is provided by the system (max. every fifteen minutes), a LocationCallback is called.每次系统提供新的位置更新时(最多每十五分钟一次),就会调用 LocationCallback。

As ResolvableFuture seems to be deprecated to use within the ListenableWorker's startWork-Method, I use the CallbackToFutureAdapter.由于 ResolvableFuture 似乎不推荐在 ListenableWorker 的 startWork-Method 中使用,我使用 CallbackToFutureAdapter。 Within this adapter, I initialize the LocationCallback and call the doWork-Method, which includes all the logic for requesting information.在这个适配器中,我初始化 LocationCallback 并调用 doWork-Method,其中包括请求信息的所有逻辑。

My problem: When the screen is active, everything works fine .我的问题:当屏幕处于活动状态时,一切正常 But as soon as the application runs in the background or is terminated, the LocationCallback won't be evoked.但是一旦应用程序在后台运行或终止,就不会调用 LocationCallback。 (last log: "Waiting for callback"). (最后的日志:“等待回调”)。 Please note that I removed big chunks of code below (like exception handling and details).请注意,我删除了下面的大块代码(如异常处理和详细信息)。

Here's my code within the MyWorker extends ListenableWorker startWork-Method:这是我在 MyWorker 扩展 ListenableWorker startWork-Method 中的代码:

public ListenableFuture<Result> startWork() {
        return CallbackToFutureAdapter.getFuture(completer -> {

            mLocationCallback = new LocationCallback() {

                @Override
                public void onLocationResult(LocationResult locationResult) {
                    super.onLocationResult(locationResult);

                   // Storing data to different repositories.

                    completer.set(Result.success());
                }

            };
            doWork();

            return "startSomeAsyncStuff";
        });
    } 

doWork():做工作():

public Result doWork() {

               mFusedLocationClient = LocationServices.getFusedLocationProviderClient(mContext);
                mSettingsClient = LocationServices.getSettingsClient(mContext);
                createLocationCallback();
                createLocationRequest();
                buildLocationSettingsRequest();

               startLocationUpdates();

        return Result.success();
    }

startLocationUpdates():开始位置更新():

 private void startLocationUpdates() {
        // Begin by checking if the device has the necessary location settings.
        mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
                .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
                    @Override
                    public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                        Log.i(TAG, "All location settings are satisfied.");
                        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper());
                   Log.i(TAG, "Location updated requested - Waiting for Callback");

                    }
                })

Related posts: How do I return a ListenableFuture<Result> with work manager 2.0?相关文章: 如何使用工作管理器 2.0 返回 ListenableFuture<Result>? and WorkManager: ResolvableFuture can only be called from within the same library group prefixWorkManager: ResolvableFuture 只能从同一个库组前缀中调用

So, after hours of trial and error here's the solution that finally works for me:因此,经过数小时的反复试验,这是最终对我有用的解决方案:

The trick is to add a Foreground-Service, that ensures the worker not to be cancelled, until a new Location-Update arrives from the system.诀窍是添加一个前台服务,以确保工作人员不会被取消,直到系统有新的位置更新到达。 However, you can not avoid showing a Notification during that timeframe.但是,您无法避免在该时间范围内显示通知。 I also moved the LocationCallback into the doWork-Method, just before requesting Location Updates (as you need to pass the Callback to that method.)在请求位置更新之前,我还将 LocationCallback 移到了 doWork 方法中(因为您需要将回调传递给该方法。)

This solution is based on https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running , which I should have taken a look at in the beginning to save me some frustration;)该解决方案基于https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running ,我应该在一开始就看一下,以免让我感到沮丧;)

Here's the startWork()-Method:这是 startWork() 方法:

 public ListenableFuture<Result> startWork() {
        return CallbackToFutureAdapter.getFuture(completer -> {
            setForegroundAsync(createForegroundInfo());

            doWork(completer);

            // This value is used only for debug purposes: it will be used
            // in toString() of returned future or error cases.
            return "Location Worker";
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用LocationCallback类 - Cannot use LocationCallback class LocationCallback 没有被调用 - LocationCallback not getting called FusedLocationProviderClient 和 LocationCallback() 对象查询 - FusedLocationProviderClient and LocationCallback() object queries LocationListener LocationCallback有什么区别? - What is the difference LocationListener LocationCallback? requestLocationUpdates 不调用 locationCallback - requestLocationUpdates doesn't call the locationCallback 为什么离子键盘事件不触发? - Why are the Ionic keyboard events not firing? 我是否需要为 fusedLocationProviderClient 取消注册 locationcallback? - Should i need to unregister locationcallback for fusedLocationProviderClient? FusedLocation 不会触发 android api &lt;= 21 的 locationcallback - FusedLocation not trigger locationcallback for android api <= 21 初始化 LocationCallback 后打开设备 GPS 时未调用 LocationCallback onLocationResult - LocationCallback onLocationResult isn't getting called when device GPS is turned on after initializing LocationCallback 为什么我的Android警报管理器立即触发? - Why is my android alarm manager firing instantly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM