2017-06-02 15 views
0

我正在製作一個需要在後臺統計步驟的應用程序。在JobService類中,我想連接到GoogleFit API並閱讀步驟。我遇到的問題是我無法讓GoogleFit連接。我的日誌「工作」顯示,但沒有出現適合連接的日誌,這導致我相信連接永遠不會發生。我嘗試在onCreate()和onStartJob()中啓動客戶端構建器,但都沒有工作。有任何想法嗎?在非活動/片段類中使用GoogleFit API

public class BackgroundStepTracker extends JobService implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks{ 
public static final String JOB_TAG = BackgroundStepTracker.class.getName(); 

private GoogleApiClient mClient = null; 
public static final String TAG = "BACKGROUND"; 
private Context _context; 
private SharedPreferences _sharedPreferences; 


private static final String SHARED_PREFERENCES = "SHAREDPREFERENCES"; 


public BackgroundStepTracker(){ 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    _sharedPreferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); 

    _context = getApplicationContext(); 

} 

@Override 
public boolean onStartJob(JobParameters job) { 

    Log.d(TAG, "Working"); 

    mClient = new GoogleApiClient.Builder(_context) 
      .addApi(Fitness.RECORDING_API) 
      .addApi(Fitness.HISTORY_API) 
      .addApi(Fitness.SENSORS_API) 
      .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE)) 
      .build(); 

    return false; 
} 

@Override 
public boolean onStopJob(JobParameters job) { 

    Log.d(TAG, "Stopping"); 

    return false; 
} 

public void subscribe() { 
    // To create a subscription, invoke the Recording API. As soon as the subscription is 
    // active, fitness data will start recording. 
    Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_STEP_COUNT_CUMULATIVE) 
      .setResultCallback(new ResultCallback<Status>() { 
       @Override 
       public void onResult(Status status) { 
        if (status.isSuccess()) { 
         if (status.getStatusCode() 
           == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) { 
          Log.i(TAG, "Existing subscription for activity detected."); 
         } else { 
          Log.i(TAG, "Successfully subscribed!"); 
         } 
        } else { 
         Log.w(TAG, "There was a problem subscribing."); 
        } 
       } 
      }); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    Log.i(TAG, "Connected!!!"); 
    // Now you can make calls to the Fitness APIs. What to do? 
    // Subscribe to some data sources! 
    subscribe(); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    // If your connection to the sensor gets lost at some point, 
    // you'll be able to determine the reason and react to it here. 
    if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) { 
     Log.w(TAG, "Connection lost. Cause: Network Lost."); 
    } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { 
     Log.w(TAG, "Connection lost. Reason: Service Disconnected"); 
    } 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 
} 
+0

onConnectionFailed方法爲空。也許在該方法的主體中添加一條日誌語句會揭示出一條有用的線索。 –

+0

我在onConnectionFailed()中添加了一個日誌,它永遠不會顯示。不確定是否嘗試連接。 – MrBovineJoni

回答

2

我的錯誤是在構建它之後沒有調用mClient.connect()。這解決了我的問題。