打开联系人时如何直接拨打电话

时间:2019-06-14 05:59:18

标签: java android apk call contacts

当我单击“按钮”时,选择一个联系人并选择一个不拨打该号码的联系人时出现的问题,当我直接选择“拨打电话”时,我想要

buttoncontact = findViewById(R.id.choosecontact);


public void choosecontact1 (View view){

     Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

   intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(intent, PICK_CONTACT)


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String call = buttoncontact.getText().toString();


    if ((requestCode == 1) && (resultCode == RESULT_OK)) {
        Cursor cursor = null;
        try {
            Uri uri = data.getData();
            cursor = getContentResolver().query(uri, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, null, null, null);
            if (cursor != null && cursor.moveToNext()) {
                String phone = cursor.getString(0);

//该代码的问题

                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(String.valueOf("tel:" + CONTENT_TYPE)));

//我想打开电话联系并直接致电

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.


                    return;

                }

                startActivity(intent);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

什么是“ CONTENT_TYPE”?难道不是您刚得到的电话号码吗?

intent.setData(Uri.parse("tel:" + phone));

答案 1 :(得分:0)

  1. 在清单文件中添加权限:    

  2. 启用电话呼叫功能

    private static final int CALL_REQUEST = 100;

     private void callPhoneNumber(){
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                   ActivityCompat.requestPermissions(ProfileActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_REQUEST);
    
                    return;
                }
            }
    
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:9879879879" ));
            startActivity(callIntent);
    
        }
    
  3. 在api级别23以上,需要询问调用的运行时权限,如果用户允许,它将进入以下方法:

    @Override public void onRequestPermissionsResult(int requestCode,String []权限,                                        int [] grantResults) {     如果(requestCode == CALL_REQUEST)     {         if(grantResults [0] == PackageManager.PERMISSION_GRANTED)         {             callPhoneNumber();         }         其他         {             Toast.makeText(ProfileActivity.this,“权限被拒绝”,Toast.LENGTH_SHORT).show();         }     } }