2017-09-15 69 views
2

觸發我運行一個後臺作業時,他們已經到期重新註冊地理圍欄。setResultCallback不是裏面的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; 
    } 

回答

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(); 
       } 
相關問題