我试图用下面提供的代码以编程方式调用一个数字:

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清单文件中设置了权限:
<uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission>

android studio抛出一个名为:
Missing Permissions required be intent Intent.ACTION_CALL:android.permission.CALL_PHONE

我似乎找不到这个错误发生的原因。如有任何帮助将不胜感激。

最佳答案

试试这个,
需要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();
                }
            }
        }
    }

检查棉花糖的权限
    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);
    }

08-04 03:37