android - 在 AsyncTask 中未触发注册地理围栏时的 setResultCallback

标签 android android-asynctask

我正在运行一项后台作业,以便在地理围栏过期时重新注册它们。

代码可以正常触发。通知在 onPostExecute 中触发,但永远不会访问 setResultCallback 中的代码。

我应该如何启用该回调,以便可以将 PlaceBuffer 对象发送到地理围栏类,从而进行添加和注册?

 @Override
    public boolean onStartJob(final JobParameters job)
    {
        mBackgroundTask = new AsyncTask<Object, Void, List<String>>()
        {
            @Override
            protected List<String> doInBackground(Object... params)
            {
                Context context = GeofenceRegistrationFirebaseJobService.this;

                // get all places in the database
                Uri uri = PlaceContract.PlaceEntry.CONTENT_URI;
                Cursor cursor = context.getContentResolver().query(
                        uri,
                        null,
                        null,
                        null,
                        null);

                if (cursor == null || cursor.getCount() == 0) return null;

                List<String> placesIds = new ArrayList<>();

                cursor.moveToPosition(-1);
                while (cursor.moveToNext())
                {
                    placesIds.add(cursor.getString(cursor
                            .getColumnIndex(PlaceContract.PlaceEntry.COLUMN_PLACE_ID)));
                }
                cursor.close();
                return placesIds;
            }

            @Override
            protected void onPostExecute(List<String> placesIds)
            {
                Context context = GeofenceRegistrationFirebaseJobService.this;

                // Build up the LocationServices API client
                googleApiClient= new GoogleApiClient.Builder(context)
                        .addApi(LocationServices.API)
                        .addApi(Places.GEO_DATA_API)
                        .build();

                geofencing = new Geofencing(context, googleApiClient);

                PendingResult<PlaceBuffer> placeResult =
                        Places.GeoDataApi.getPlaceById(googleApiClient,
                                placesIds.toArray(new String[placesIds.size()]));

                placeResult.setResultCallback(new ResultCallback<PlaceBuffer>()
                {
                    @Override
                    public void onResult(@NonNull PlaceBuffer places)
                    {    
                        geofencing.addUpdateGeofences(places);
                        geofencing.registerAllGeofences();
                    }
                });



                // when the job is finished, issue a notification if is set in preferences
                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
                // does user have notifications enabled?
                boolean wantNotif =
                        pref.getBoolean(context
                                .getString(R.string.pref_activate_notification_key), true);
                if(wantNotif)
                {
                    BackgroundTasks.executeTask(context,
                            BackgroundTasks.ACTION_NOTIFY_USER_GEOFENCES_REGISTERED);
                }



                jobFinished(job, false);
            }
        };
        mBackgroundTask.execute();
        return true; 
    }

最佳答案

已找到解决方案。令人惊讶的是,有时一个简单的解决方案看起来不可能找到。构建 API 客户端时,添加回调并CONNECT客户端。

// Build up the LocationServices API client
                googleApiClient= new GoogleApiClient.Builder(context)
                        .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks()
                        {
                            @Override
                            public void onConnected(@Nullable Bundle bundle)
                            {  // add your stuff, we are connected
                            }

                            @Override
                            public void onConnectionSuspended(int i) {
                                googleApiClient.connect();
                            }
                        })
                        .addApi(LocationServices.API)
                        .addApi(Places.GEO_DATA_API)
                        .build();

                // CONNECT the client 
                if (!googleApiClient.isConnecting() || !googleApiClient.isConnected())
                {
                    googleApiClient.connect();
                }

关于android - 在 AsyncTask 中未触发注册地理围栏时的 setResultCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46247171/

相关文章:

android - 使用 android maps api v2,如何跟踪用户位置(类似于 google maps api 跟踪方式)。用户位置标记始终保持在中心

java - 非法状态异常 AsyncTask MediaPlayer

android - 在方向更改期间将网格数据保持在 fragment 中的最佳方法

android - 异步图像下载并存储在 ImageView 数组中

java - 如何将 Fragment 的上下文转换为界面

java - 打开新 Intent 时 Android 应用程序崩溃

java - Android Oreo 动画渲染问题

android - 在第二个 Activity 中显示捕获的屏幕截图(在 ImageView 或图库中)

android imageView 不喜欢我的 .png 文件

php - 如何使用php搜索该字符串是否作为android中mysql数据库中的值存在?