来自v4.Fragment Lollipop的onRequestPermissionsResult

时间:2016-05-27 00:34:27

标签: android android-fragments

我在Activity中有一个嵌套的Fragment,我正在尝试为ACCESS_FINE_LOCATION& ACCESS_COARSE_LOCATION。

在Android.M中,它在同一片段中运行良好的回调,但在棒棒糖中回调来了,但请求代码为空。

这是我在MainActivity中的片段

private static final int MY_LOC_PERMISSIONS = 123;
private static GoogleMap mMap;
private LocationManager locationManager;

private void setUpMap() {
    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //Tried this from Fragment 
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_LOC_PERMISSIONS);
            //Tried this from Activity -> calls to Activity.onRequestPermissionsResult
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_LOC_PERMISSIONS);
    }else{
        locationManager.removeUpdates(this);
    }
}

和同一片段中的请求回调

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_LOC_PERMISSIONS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                setUpMap();
            } else {
                // permission denied, boo! Disable the functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other permissions this app might request
    }
}

我也从活动回调中尝试了这个但是同样的问题无限循环,因为请求代码在从SO帖子Request runtime permissions from v4.Fragment and have callback go to Fragment?

中的片段中收到时为空
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {
            fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

我从一个像魅力一样的活动中尝试了同样的事情。但我需要这个片段。我还创建了一个函数,我可以检查我的Activity中的函数,但是当我尝试在Fragment中执行相同操作时,它甚至不让我编译,就像在Activity中一样

private boolean isRequestRequired() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    return true;
}
if (checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
    return true;
}
if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
    Snackbar.make(findViewById(R.id.fab), "Please allow required access", Snackbar.LENGTH_INDEFINITE)
            .setAction(android.R.string.ok, new View.OnClickListener() {
                @Override
                @TargetApi(Build.VERSION_CODES.M)
                public void onClick(View v) {
                    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS);
                }
            }).show();
} else {
    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS);
}
return false;
}

在我正在检查的活动中

if (!isRequestRequired()) {
    return;
}else{
  SmsManager sms = SmsManager.getDefault();
  sms.sendTextMessage(<NUMBER>,null,<TEXT>,null,null);
}

但Android平板电脑中的片段同样错误:“你必须检查请求权限......”

0 个答案:

没有答案