1

这是我的代码,请检查并给我解决方案。我也使用这两个依赖项。我已经使用 Fitnessoption 来获得用户的许可。在获得许可后,我还订阅了用户,然后我使用 readData() 从谷歌健身 api 读取每日步数数据。我还检查了清单文件的活动识别权限。

implementation 'com.google.android.gms:play-services-fitness:18.0.0'

implementation 'com.google.android.gms:play-services-auth:18.0.0'

//manifest file
     
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
    
public class HomeFragement extends Fragment{    

// FitnessOptions create

FitnessOptions fitnessOptions = FitnessOptions.builder()
                    .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                    .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                    .build();

    GoogleSignInAccount account = GoogleSignIn.getAccountForExtension(getActivity(), fitnessOptions);

 

    if (!GoogleSignIn.hasPermissions(account, fitnessOptions)) {
                    GoogleSignIn.requestPermissions(
                            getActivity(),
                            REQUEST_OAUTH_REQUEST_CODE,
                            account,
                            fitnessOptions);
                } else {
                    subscribe();
                }

    // subscribe the google api    

    public void subscribe() {
            // To create a subscription, invoke the Recording API. As soon as the subscription is
            // active, fitness data will start recording.
            Fitness.getRecordingClient(getActivity(), GoogleSignIn.getLastSignedInAccount(getContext()))
                    .subscribe(DataType.TYPE_STEP_COUNT_CUMULATIVE)
                    .addOnCompleteListener(
                            new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        Log.i(TAG, "Permission Give Successfully!");
                                    } else {
                                        Log.w(TAG, "There was a problem subscribing.", task.getException());
                                    }
                                }
                            });
        }

        // read daily step data
    
        private void readData() {
            Fitness.getHistoryClient(getActivity(), GoogleSignIn.getLastSignedInAccount(getContext()))
                    .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();
                                    txtTodayStep.setText(String.valueOf(total));
                                    txtTodayCoin.setText(String.valueOf(new DecimalFormat("#.##").format(((int)(total)/1000f))));
                                    Log.i(TAG, "Total steps: " + total);
                                }
                            })
                    .addOnFailureListener(
                            new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.w(TAG, "There was a problem getting the step count.", e);
                                }
                            });
        }

}

    This is my code and what is problem please tell me.
4

0 回答 0