【问题标题】:How can I implement CALL intent in android?如何在 android 中实现 CALL 意图?
【发布时间】:2016-03-16 08:47:07
【问题描述】:

这就是我现在正在做的事情。我收到以下错误

android.content.ActivityNotFoundException:找不到要处理的活动 意图 { act=android.intent.action.CALL dat=16477210790 }

 @Override
    public void onClick(View v) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(store_contact_no.getText().toString()));
        startActivity(callIntent);

    }

【问题讨论】:

  • 您是否提供了必要的权限?
  • 您添加了<uses-permission android:name="android.permission.CALL_PHONE" /> 吗?如果是,您是否尝试捕获此异常并改用ACTION_DIAL
  • 权限已添加。我需要 ACTION_CALL 而不是 ACTION_DIAL

标签: android android-intent


【解决方案1】:

您不需要在清单中声明拨号意图过滤器,也不需要任何 ACTION_DIAL 权限。寻找我的实现

private void startDialActivity(String phone){
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+phone));
startActivity(intent);}

也可以检查设备是否支持电话

private boolean isTelephonyEnabled(){
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
return tm != null && tm.getSimState()==TelephonyManager.SIM_STATE_READY;}

使用此代码。 这可能会对您有所帮助。

【讨论】:

    【解决方案2】:

    将此添加到您的清单中

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

    在Activity检查权限中,

    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1);
            }
        } else {
            Call(sNumber);
        }
    

    其中 sNumber 是字符串中的数字

    然后覆盖 onRequestPermissionsResult,

        @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    
        switch (requestCode) {
            case 100:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted,
                    Call(sNumber);
    
                } else {
    
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
    
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
    
                        requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1);
    
                    }
                }
                break;
        }
    }
    

    添加此方法,

     private void Call(String sNumber)
    {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + sNumber));
        startActivity(callIntent);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      相关资源
      最近更新 更多