2016-10-20 13 views
5

मैं पृष्ठभूमि Service से स्थान प्राप्त करने का प्रयास कर रहा हूं। समस्या यह है कि Awareness.SnapshotApi.getLocation(mGoogleApiClient).setResultCallback(new ResultCallback<LocationResult>() के अंदर फ़ंक्शन को कॉल नहीं किया जा रहा है। क्या गलत हो सकता है?onResult() जागरूकता में। स्नैपशॉटएपीआई.get स्थान किसी सेवा से नहीं बुलाया जा रहा है

फ़ंक्शन onConnected() और onConnectionSuspended() भी कॉल नहीं किए जा रहे हैं, लेकिन जब मैं mGoogleApiClient.isConnected() = true प्रिंट करता हूं तो वापस आ जाता है।

तो मैं समझने की कोशिश कर रहा हूं कि onConnected() को क्यों नहीं कहा जा रहा है।

public class BlockingService extends Service implements GoogleApiClient.ConnectionCallbacks { 
    private GoogleApiClient mGoogleApiClient; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     initAwarenessAPI(); 
     amIClosetoLocation(<LatLng I get from my database>); 

     return START_NOT_STICKY; 
    } 

    @Override 
    public void onDestroy() { 

     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
     super.onDestroy(); 
    } 

    private void initAwarenessAPI() { 
     Context context = getApplicationContext(); 
     mGoogleApiClient = new GoogleApiClient.Builder(context) 
       .addApi(Awareness.API) 
       .build(); 
     mGoogleApiClient.connect(); 
    } 

    private Boolean amIClosetoLocation(final LatLng savedLoc) { 
     final String TAG = "Awareness"; 
     mLog.printToLog(className + Constants.funcStart + MethodName.methodName() + ": Entered Function"); 
     amIClosetoLocation = false; 

     if (ContextCompat.checkSelfPermission(
       getApplicationContext(), 
       android.Manifest.permission.ACCESS_FINE_LOCATION) != 
       PackageManager.PERMISSION_GRANTED) { 
      mLog.printToLog(className + MethodName.methodName() + ": ***DONT HAVE LOCATION PERMISSION***"); 
     } else { 
      mLog.printToLog(className + MethodName.methodName() 
        + ": Permission Exists -- so now going to get location"); 

      Awareness.SnapshotApi.getLocation(mGoogleApiClient) 
        .setResultCallback(new ResultCallback<LocationResult>() { 
         @Override 
         public void onResult(@NonNull LocationResult locationResult) { 
          //THIS FUNCTION IS NOT GETTING CALLED 
          if (!locationResult.getStatus().isSuccess()) { 
           Log.e(TAG, "Could not get location."); 

           mLog.printToLog(className + MethodName.methodName() + ": Could NOT get location"); 

           return false; 
          } else { 
           mLog.printToLog(className + MethodName.methodName() 
             + ": Success: Able to send location"); 
          } 
          Location currLocation = locationResult.getLocation(); 

          mLog.printToLog(className + MethodName.methodName() 
            + ": Success, Received location = " + currLocation.toString()); 

          mLog.printToLog(className + Constants.funcStart + MethodName.methodName() + 
            ": Got Location = Lat: " + 
            currLocation.getLatitude() + 
            ", Lon: " + currLocation.getLongitude()); 
         } 
        }); 
     } 


     mLog.printToLog(className + Constants.funcEnd + MethodName.methodName() 
       + ": Exiting Function, returning amIClosetoLocation = " + amIClosetoLocation); 


     return amIClosetoLocation; 
    } 


@Override 
public void onConnected(@Nullable Bundle bundle) { 

    mLog.printToLog(className + Constants.funcStart + MethodName.methodName() + ": Entered Function"); 


@Override 
public void onConnectionSuspended(int i) { 

} 
} 

उत्तर

1

new GoogleApiClient.Builder(context) दूसरा पैरामीटर कॉलबैक श्रोता है और आपने इसे याद किया है।

मेरा मतलब है: new GoogleApiClient.Builder(context, this)

+0

कॉल में दो संदर्भ हैं? मैंने आपके समाधान की कोशिश की और इसे प्राप्त किया: http://imgur.com/a/uh5Y6 – user1406716

+0

श्रोता – eduyayo

+0

लागू करने वाले को पास करें मुझे यह क्यों नहीं मिला है? – eduyayo

संबंधित मुद्दे