【问题标题】:How to make a phone call using intent in Android without permission?如何在未经许可的情况下在Android中使用意图拨打电话?
【发布时间】:2020-04-19 04:09:52
【问题描述】:

我的应用使用 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)));
            }
        }
    }

【问题讨论】:

  • 您需要从清单中删除权限标签,然后从类中删除与权限相关的代码(checkSelfPermission & requestPermissions...),然后只使用 startActivity 为 Intent.ACTION_DIAL,检查下面的代码或者它也是在你的 if (Build.VERSION.SDK_INT

标签: android android-intent android-phone-call


【解决方案1】:

你可以这样做:

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

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

【讨论】:

  • 感谢此代码替换为我的代码中的什么代码?
  • 没关系。随心所欲。此外,您不需要授予运行时权限。 @adp
【解决方案2】:

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

/**
 * 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()
    }
}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2020-07-29
      • 2017-12-15
      相关资源
      最近更新 更多