与 android 应用同步权限 Google-Fit

sync permissions with an android app Google-Fit

我正在使用 android studio 创建一个应用程序,我正在使用 Google Fit 应用程序同步这两个应用程序以通过 Mi-Fit 获得一些测量值。问题是获取权限 window 没有出现在我的 phone 中,我不知道为什么。这是我的许可 class:

package Model.Users;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInOptionsExtension;
import com.google.android.gms.fitness.FitnessOptions;

import static com.google.android.gms.fitness.data.DataType.TYPE_ACTIVITY_SEGMENT;
import static com.google.android.gms.fitness.data.DataType.TYPE_CALORIES_EXPENDED;
import static com.google.android.gms.fitness.data.DataType.TYPE_DISTANCE_DELTA;
import static com.google.android.gms.fitness.data.DataType.TYPE_STEP_COUNT_DELTA;

public class Permissions {

    private AppCompatActivity app;
    final private int ALL_PERMISSIONS = 101;

    public Permissions(AppCompatActivity app) {
        this.app = app;
    }

    /**
     *
     * @throws PackageManager.NameNotFoundException
     */
    public void requestPermissions() throws PackageManager.NameNotFoundException {
        PackageInfo info = app.getPackageManager().getPackageInfo(app.getPackageName(), PackageManager.GET_PERMISSIONS);
        String[] permissions = info.requestedPermissions;//This array contains the requested permissions.

        Log.i("PERMISSIONS", "************PERMISSIONS LIST*************");

        ActivityCompat.requestPermissions(app, permissions, ALL_PERMISSIONS); /** gets all permissions from manifest! */

        // insert all permissions needed
        GoogleSignInOptionsExtension fitnessOptions =
                FitnessOptions.builder()
                        .addDataType(TYPE_STEP_COUNT_DELTA,FitnessOptions.ACCESS_READ)
                        .addDataType(TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
                        .addDataType(TYPE_CALORIES_EXPENDED,FitnessOptions.ACCESS_READ)
                        .addDataType(TYPE_ACTIVITY_SEGMENT,FitnessOptions.ACCESS_READ)
                        .build();

        if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(app), fitnessOptions)) {
            GoogleSignIn.requestPermissions( app,1, GoogleSignIn.getLastSignedInAccount(app),
                    fitnessOptions);
            Log.i("PERMISSIONS", "************PERMISSIONS REQUESTED*************");

        }
    }
}

我得到的第一个屏幕是选择 google 帐户: 总是那么我想得到这个屏幕,但我几乎从不这样做:

然后弹出批准位置的最终屏幕:

无法弄清楚为什么第二个屏幕不显示,我必须获得这些许可才能管理 google-fit

收集的数据的使用

谢谢。

将您的逻辑放在它自己的函数中,在 onCreate() 之外。

private void youLogic() {
    // some code
}

如果权限被授予,直接在onCreate()中调用该函数即可:

@Override
public void onCreate(Bundle savedInstanceState) {
    //...
    if (checkSelfPermissions(/*whatever*/) {
        youLogic();
    } else {
        requestPermissions(/*whatever*/);
    }

    //nothing dependent on runtime permissions after this point
}

然后覆盖 onRequestPermissionsResult():

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == /*whatever you set in requestPermissions()*/) {
        if (!Arrays.asList(grantResults).contains(PackageManager.PERMISSION_DENIED)) {
            //all permissions have been granted
            youLogic(); //call your dependent logic
        }
    }
}

您还可以检查清单中的所有权限:

if(permissions.Contains(info)) {
  
  // request all other permissions here
  .
  .
  .
}