有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android调用意图权限

我正在尝试在我的应用程序中实现调用函数,我有以下代码:

Intent phoneCall = new Intent(Intent.ACTION_CALL);
phoneCall.setData(Uri.parse("123456"));
startActivity(phoneCall);

我在清单文件中添加了所需的权限,但它仍然给我以下错误:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException` less... (Ctrl+F1) 
This inspection looks at Android API calls and ensures that the correct type of resource is passed to an int-parameter expecting resources of a given type; it checks that APIs which expect an RGB color integer are passed actual colors rather than color resources; it checks that APIs which require a certain permission have the permission declared in the manifest; it checks that parameters expected to fall within a given range actually do; it checks that results of certain method calls are looked at by the caller, and so on.

你知道我如何解决这个问题吗://


共 (2) 个答案

  1. # 1 楼答案

            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
                        REQUEST_CODE_ASK_PERMISSIONS);
            } else {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "your number"));
                startActivity(intent);
            }
    
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Call Permission Granted..Please dial again.", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Call permission not granted", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    
  2. # 2 楼答案

    因为您的目标Sdk版本是23,所以您需要在运行时询问用户权限。尽管您仍然需要在Manifest.xml中包含权限

    下面是一些很棒的入门教程

    Tutorial 1

    Tutorial 2