简体   繁体   English

与 android 应用 Google-Fit 同步权限

[英]sync permissions with an android app Google-Fit

I'm creating an app with android studio and I'm using the Google Fit app to sync the two to get some measurements through Mi-Fit.我正在使用 android 工作室创建一个应用程序,并且我正在使用 Google Fit 应用程序来同步两者以通过 Mi-Fit 获得一些测量结果。 problem is that the getting permissions window isn't popping in my phone and I can't figure out why.问题是获取权限 window 没有出现在我的手机中,我不知道为什么。 this is my Permission class:这是我的许可 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*************");

        }
    }
}

the first screen I get is choosing google account:我得到的第一个屏幕是选择谷歌帐户:弹出这个画面 always then I suppose to get this screen but I almost never do:我总是想得到这个屏幕,但我几乎从不这样做:几乎从不弹出

and then the final screen of approving location pops up:然后弹出批准位置的最终屏幕:来自手机的权限总是弹出

can't figure out why the 2nd screen doesn't show and I have to get those permission to manage the use of the data collected by the google-fit无法弄清楚为什么第二个屏幕没有显示,我必须获得这些权限才能管理 google-fit 收集的数据的使用

Thanks.谢谢。

Put your logic in its own function(s), outside of onCreate().将您的逻辑放在 onCreate() 之外的自己的函数中。

private void youLogic() {
    // some code
}

If the permissions are granted, just call that function directly from onCreate():如果授予权限,只需直接从 onCreate() 调用 function:

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

    //nothing dependent on runtime permissions after this point
}

Then override onRequestPermissionsResult():然后覆盖 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
        }
    }
}

You may also check all permissions in manifest:您还可以检查清单中的所有权限:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM