0

我的应用程序使用 android 权限 action_dial。如何更改代码使其不需要许可?我需要能够在没有许可的情况下调用

这是代码:

这不是我的错,这篇文章充满了代码。


    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // Older Version No need to request Permission
        String dial = "tel:" + phoneNo;
        fragment.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(dial)));
    } else {
        // Need to request Permission
        if (fragment.getActivity() != null) {
            if (ContextCompat.checkSelfPermission(fragment.getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                fragment.requestPermissions(new String[]{
                        Manifest.permission.CALL_PHONE
                }, Constants.REQUEST_CODE__PHONE_CALL_PERMISSION);
            } else {
                String dial = "tel:" + phoneNo;
                fragment.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(dial)));
            }
        }
    }
}


                                             int[] grantResults, Fragment fragment, String phoneNo) {
    if (requestCode == Constants.REQUEST_CODE__PHONE_CALL_PERMISSION) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            callPhone(fragment, phoneNo);
        } else {
            Utils.psLog("Permission not Granted");
        }


    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // Older Version No need to request Permission
        String dial = "tel:" + phoneNo;
        activity.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
    } else {
        // Need to request Permission
        if (activity != null) {
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                activity.requestPermissions(new String[]{
                        Manifest.permission.CALL_PHONE
                }, Constants.REQUEST_CODE__PHONE_CALL_PERMISSION);
            } else {
                String dial = "tel:" + phoneNo;
                activity.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
            }
        }
    }

4

3 回答 3

1

你可以这样做:

Intent intentDial = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "123456789"));
context.startActivity(intentDial);

但它会将用户导航到默认呼叫拨号器应用程序并将电话号码传递给它,因此用户应该按下呼叫按钮拨打电话。它不会直接拨打电话。

于 2019-12-30T12:04:01.357 回答
0

您不需要权限即可执行该电话呼叫意图。

/**
 * Make a phone call. Send to the phone app
 *
 * @param phoneNumber the phone number to call
 */
private fun performPhoneCall(phoneNumber: String) {
    val intent = Intent(Intent.ACTION_DIAL)
    intent.data = Uri.parse("tel:$phoneNumber")
    try {
        context.startActivity(intent)
    } catch (ex: Exception) {
        displayErrorMessage()
    }
}
于 2021-11-10T20:49:19.893 回答
0

未经许可不得直接拨打电话。您可以使用所需号码打开应用程序顶部的拨号器,用户接下来应单击绿色呼叫按钮。为此,请使用此代码

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumberStr));

startActivity(callIntent);
于 2019-12-30T12:12:42.110 回答