注册Geofences时的setResultCallback不会在AsyncTask中触发

时间:2017-09-15 20:38:30

标签: android android-asynctask

我正在运行一份后台工作,在Geofences过期后重新注册。

代码被触发正常。通知在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; 
    }

1 个答案:

答案 0 :(得分:0)

找到解决方案。令人惊讶的是,简单的解决方案有时无法找到。构建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();
                }