简体   繁体   中英

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. problem is that the getting permissions window isn't popping in my phone and I can't figure out why. this is my Permission 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

Thanks.

Put your logic in its own function(s), outside of onCreate().

private void youLogic() {
    // some code
}

If the permissions are granted, just call that function directly from onCreate():

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

    //nothing dependent on runtime permissions after this point
}

Then override 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
  .
  .
  .
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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