3
我無法獲取使用Google Fit Api的距離。我使用了類似的計步器,它的工作。它只是說聽衆註冊。Fit Api中的距離
大部分代碼來自Github示例。
什麼可能是錯的?
public class MainActivity extends Activity {
public static final String TAG = "BasicSensorsApi";
private static final int REQUEST_OAUTH = 1;
private OnDataPointListener mListener;
private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;
TextView dispSteps;
long numSteps;
private GoogleApiClient mClient = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (savedInstanceState != null) {
authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
}
dispSteps=(TextView)findViewById(R.id.dispStepsTV);
numSteps=0;
buildFitnessClient();
}
private void updateDispSteps(final int update) {
numSteps+=update;
runOnUiThread(new Runnable() {
@Override
public void run() {
dispSteps.setText(String.valueOf(numSteps));
}
});
}
private void buildFitnessClient() {
// Create the Google API Client
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_DISTANCE_DELTA)
.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.i(TAG, "There was a problem subscribing.");
}
}
});
mListener = new OnDataPointListener() {
@Override
public void onDataPoint(DataPoint dataPoint) {
for (Field field : dataPoint.getDataType().getFields()) {
Value value = dataPoint.getValue(field);
updateDispSteps(value.asInt());
Log.i("BIG BLUE TEXT", value.toString());
}
}
};
SensorRequest req = new SensorRequest.Builder()
.setDataType(DataType.TYPE_DISTANCE_DELTA)
.setSamplingRate(10, TimeUnit.SECONDS)
.build();
Fitness.SensorsApi.add(mClient, req, mListener)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Listener registered!");
} else {
Log.i(TAG, "Listener not registered.");
}
}
});
}
@Override
public void onConnectionSuspended(int i) {
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG, "Connection lost. Reason: Service Disconnected");
}
}
}
)
.addOnConnectionFailedListener(
new GoogleApiClient.OnConnectionFailedListener() {
// Called whenever the API client fails to connect.
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed. Cause: " + result.toString());
if (!result.hasResolution()) {
// Show the localized error dialog
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
MainActivity.this, 0).show();
return;
}
if (!authInProgress) {
try {
Log.i(TAG, "Attempting to resolve failed connection");
authInProgress = true;
result.startResolutionForResult(MainActivity.this,
REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG,
"Exception while starting resolution activity", e);
}
}
}
}
)
.build();
}
PS:這是我的第一個計算器問題:P
,如果你能得到步數則u墊將其轉換成長度。大約1300步驟等於1公里 –