gpt4 book ai didi

java - 使用 Google Fit API 获取 StepCount 失败

转载 作者:行者123 更新时间:2023-11-29 02:30:03 25 4
gpt4 key购买 nike

成功登录 Google Fit 帐户但未显示步数。请帮忙

public class MainActivity extends AppCompatActivity {

public static final int GOOGLE_FIT_PERMISSIONS_REQUEST_CODE=101;
public static final String TAG="StepCounter";
private static final int REQUEST_OAUTH = 1;

private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;

private GoogleApiClient mClient = null;
ProgressBar progressBar;
public static Boolean loggedIn=false;


@Override
protected void onStart() {
super.onStart();
// Connect to the Fitness API
Log.i(TAG, "Connecting...");
mClient.connect();
}

@Override
protected void onStop() {
super.onStop();
if (mClient.isConnected()) {
mClient.disconnect();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_OAUTH) {
authInProgress = false;
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mClient.isConnecting() && !mClient.isConnected()) {
mClient.connect();
}
}
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(ProgressBar)findViewById(R.id.progressBar);



if (savedInstanceState != null) {
authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
}

buildFitnessClient();


new Thread(new Runnable() { //worker thread adding data to the array list(the data can be from database)



@Override
public void run() {



while(loggedIn) {

readData();


try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}


}
}).start();







}

private void buildFitnessClient() {

mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {

@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
progressBar.setVisibility(View.INVISIBLE);
loggedIn=true;
// Now you can make calls to the Fitness APIs.
// Put application specific code here.











}

@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.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;
}
// The failure has a resolution. Resolve it.
// Called typically when the app is not yet authorized, and an
// authorization dialog is displayed to the user.
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();







}

The function for reading the stepcount from googlefit account

  private void readData() {

Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener(
new OnSuccessListener<DataSet>() {
@Override
public void onSuccess(DataSet dataSet) {
long total =
dataSet.isEmpty()
? 0
: dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
Log.i(TAG, "Total steps: " + total);
Toast.makeText(getApplicationContext(),String.valueOf(total),Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "There was a problem getting the step count.", e);
Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_SHORT).show();
}
});
}




}

When successfully login then readData() is called but no Log msg or Toast message is getting.

在调试 readData() 代码时正在执行但没有显示结果。

最佳答案

我正在附加一个 OAuth2.0 范围。确保您已选择所有这些。

enter image description here

关于java - 使用 Google Fit API 获取 StepCount 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49993854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com