繁体   English   中英

如果在Android Studio中按下按钮,则无法拨打电话

[英]Can't dial a phone on the event of a button press in Android Studio

在我的android应用中,我有一个带有几个按钮的导航抽屉,这些按钮可以打开具有相关布局的不同片段。

该按钮中的一个应该调用一个功能,该功能理论上应该执行电话拨号。 它似乎不起作用。 什么也没发生,导航抽屉刚刚关闭。 我已经在清单中实现了此许可权,为了获得许可权,该清单应该起作用:

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

我的代码基于导航抽屉中的按钮事件执行代码,如下所示:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    android.app.FragmentManager fragmentmanager = getFragmentManager();

    if (id == R.id.nav_forside) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new MainFragment())
                .commit();

    } else if (id == R.id.nav_matif) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new MatifFragment())
                .commit();

    } else if (id == R.id.nav_om) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new OmFragment())
                .commit();

    } else if (id == R.id.nav_rk) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new RkFragment())
                .commit();

    } else if (id == R.id.nav_bd) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new BdFragment())
                .commit();

    } else if (id == R.id.nav_rf) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new RfFragment())
                .commit();

    } else if (id == R.id.nav_kontakt_os) {
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new KontaktOsFragment())
                .commit();

    } else if (id == R.id.nav_call_number) {
    callNumber();

    }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

最后但并非最不重要的一点是,执行电话呼叫的功能如下所示: 我必须指出,如果我不检查程序包管理器是否已授予访问权限以执行电话呼叫,则会引发错误。 这就是为什么代码检查是否授予权限的原因:

public void callNumber () {
            //Tries to call the phone number, and catches any errors that might be present
            try {
                //Checking if the the permission to call a phone is granted
                if (ActivityCompat.checkSelfPermission(MainActivity.this.getApplicationContext(), Manifest.permission.CALL_PHONE) ==
                        PackageManager.PERMISSION_GRANTED) {

                    Intent i = new Intent(Intent.ACTION_DIAL);
                    String p = "tel:12345678";
                    i.setData(Uri.parse(p));
                    startActivity(i);
                }
            } catch (Exception exLog) {
                //Catches error that prevents phone from calling, and expresses itself in form of a alertdialog
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Opkaldet kunne ikke gennemføres");
                alertDialog.setMessage(exLog.toString());
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();
            }
        }

我知道Amiars的答案有效,但这并不是正确的答案。

问题是:您不需要Intent.ACTION_DIAL任何许可。 您需要权限CALL_PHONE才能使用Intent.ACTION_CALL

不同之处在于ACTION_CALL实际上执行了呼叫,这有些危险,因为它通常会给用户带来费用,而ACTION_DIAL仅打开拨号程序,并且用户将通过按下呼叫按钮来明确完成操作。 全部在文档中: https : //developer.android.com/reference/android/content/Intent.html

ACTION_CALL

活动行动:向某人打电话(...)

注意:哪些应用程序可以发起呼叫受到限制; 大多数应用程序应使用ACTION_DIAL

注意:如果您的应用定位到M或更高版本,并且声明使用的CALL_PHONE权限未被授予,则尝试使用此操作将导致SecurityException。

ACTION_DIAL

活动操作:拨打数据指定的号码。 这显示了带有拨号号码的用户界面,允许用户明确发起呼叫

因此正确的答案是:

if有权限, if删除,然后直接调用startActivity(i);

编辑:

我只是在最新的AndroidStudio上键入了该代码,而未对清单进行任何许可,它可以正常工作。 无警告,在设备上执行时将打开拨号程序。

 public static void call(Context context, String number) {
    Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
    try {
      context.startActivity(i);
    } catch (Exception e) {
      // this can happen if the device can't make phone calls
      // for example, a tablet
    }
  }

我还可以指出其他几个问相同问题的问题:

如何打开显示电话号码的拨号器?

使用Intent拨打电话时需要许可吗?

如何在Android中使用选定号码在手机上打开拨号程序

意向呼叫操作不适用于棉花糖

https://stackoverflow.com/a/13123613/906362

希望对您有所帮助。

如果未授予许可怎么办?

在棉花糖之后的新版本android中,您需要像以前一样检查代码中的权限,但是如果未授予权限,则需要请求权限。 此版本android的cuz清单中的权限将无法正常工作。

if (ActivityCompat.checkSelfPermission(MainActivity.this.getApplicationContext(), Manifest.permission.CALL_PHONE) ==
                    PackageManager.PERMISSION_GRANTED) {

                Intent i = new Intent(Intent.ACTION_DIAL);
                String p = "tel:12345678";
                i.setData(Uri.parse(p));
                startActivity(i);
}
else
{
  ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CALL_PHONE}, 0);
}

您还可以在以下活动中为授予的案例权限设置一个回调:

@Override
public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults) 
{
  if(requestCode==0 && ActivityCompat.checkSelfPermission(MainActivity.this.getApplicationContext(), Manifest.permission.CALL_PHONE) ==
                PackageManager.PERMISSION_GRANTED) {
   Intent i = new Intent(Intent.ACTION_DIAL);
                String p = "tel:12345678";
                i.setData(Uri.parse(p));
                startActivity(i);
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM