Android requestPermission不显示对话框

时间:2016-07-21 11:57:48

标签: android permissions request android-6.0-marshmallow

我需要从我的申请中拨打给定的电话号码。

所以我必须验证通话权限。

但对话框显示在我的应用程序中,直接进入deny函数。我在这里做了什么问题。请查看我的代码并为此提供解决方案。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 105);
            } else {
                startActivity(callIntent);
            }
        } else {
            startActivity(callIntent);
        }

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 105:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                callJobseeker();
            } else {
                Toast.makeText(getApplicationContext(), "You cannot make a call.Because call permission is denied", Toast.LENGTH_SHORT).show();
            }
            break;
                 }
                }
清单文件中的

给出了如下许可:

<uses-permission android:name="android.permission.CALL_PHONE" />
<permission android:name="com.example.permission.CALL_PHONE"
    />

4 个答案:

答案 0 :(得分:0)

尝试使用 shouldShowRequestPermissionRationale

//Function use for request permission to access camera & gallary.
    private void requestPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
            Toast.show(context, getString(R.string.requestPermission));
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
        }
    }

答案 1 :(得分:0)

你可以这样做。使课程如下

public class CheckPermisson  {

    private Context context;

    public CheckPermisson(Context context) {
        this.context = context;
    }

    public boolean checkCallPermission() {
        if (ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            Log.i("TAG","Call Permission is granted");
          return true;


        } else {

            return false;
        }

    }
    public boolean checkExternalStoragePermission() {
        if (ContextCompat.checkSelfPermission(context.getApplicationContext(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.i("TAG", "Storage Permission is granted");
            return true;
        }
        else
        {
            return false;
        }
    }
    public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Log.i("TAG", "Location Permission is granted");
            return true;
        }
        else
        {
            return false;
        }
    }
}

您可以使用如下

CheckPermisson permisson = new CheckPermisson(getActivity());

if (Build.VERSION.SDK_INT >= 23) {
    if (permisson.checkCallPermission()) {
        itemSelectedPosition = position;
        createDialogBox(position);
    } else {
        requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, CALL_PERMISSION_REQUEST_CODE);
    }
} else {
    createDialogBox(position);
}

在清单

中添加以下权限
<uses-permission android:name="android.permission.CALL_PHONE" />

答案 2 :(得分:0)

您应该使用以下方法检查用户是否已授予CALL_PHONE权限:

if (checkSelfPermission(android.Manifest.permission. CALL_PHONE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true; 
}

答案 3 :(得分:0)

我得到了答案。

我在帖子中的错误是。,

在android清单文件中,我删除了这一行。

<permission android:name="com.example.permission.CALL_PHONE"
    />

我更改了以下代码。

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CALL_PHONE}, 105);
                } else {
                    startActivity(callIntent);
                }