缺少必需的权限是Intent.ACTION_CALL:android.permission.CALL_PHONE

时间:2017-03-24 04:46:36

标签: android android-intent permissions android-permissions

我正在尝试使用下面提供的代码以编程方式调用数字:

if(ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED)
{
    ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},CALL); 
}
else
{   
    String number="56789855";                    
    Intent intent4=new Intent(Intent.ACTION_CALL);
    intent4.setData(Uri.parse("tel:"+number));
    startActivity(intent4);
}

我也在Android Manifest File中设置了权限:

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

Android Studio会抛出一个名为:

的错误
Missing Permissions required be intent Intent.ACTION_CALL:android.permission.CALL_PHONE

我似乎无法找到发生此错误的原因。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

试试这个,

必需android.Manifest.permission.CALL_PHONE代替Manifest.permission.CALL_PHONE

    private Context mContext=YourActivity.this;

    private static final int REQUEST = 112;


    if (Build.VERSION.SDK_INT >= 23) {
        String[] PERMISSIONS = {android.Manifest.permission.CALL_PHONE};
        if (!hasPermissions(mContext, PERMISSIONS)) {
            ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
        } else {
             makeCall();
        }
    } else {
        makeCall();
    }

获取权限结果

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    makeCall();
                } else {
                    Toast.makeText(mContext, "The app was not allowed to call.", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

检查marshmallow的权限

    private static boolean hasPermissions(Context context, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

清单

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

通话功能:

    public void makeCall()
    {
        String number="56789855";                    
        Intent intent4=new Intent(Intent.ACTION_CALL);
        intent4.setData(Uri.parse("tel:"+number));
        startActivity(intent4);
    }