繁体   English   中英

在android中呼叫联系人

[英]Call a contact in android

我编写了一个使用语音识别并将语音保存到字符串中的 android 应用程序。现在我可以访问该字符串的不同部分,例如,当我说给爸爸打电话时,我需要从联系人中检索爸爸的号码。

   private void call() {
  Intent in=new Intent(Intent.ACTION_CALL,Uri.parse("0000000000"));
  try{
     startActivity(in);
  }

  catch (android.content.ActivityNotFoundException ex){
     Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
  }

}

我知道这个函数,但不是给 Uri.parse 实际号码,我需要将爸爸的号码传递给它。例如像 Uri.parse(dad.PhoneNumber) 这样的东西。如何使用姓名从联系人中检索电话号码? 谢谢。

您需要从只知道姓名的联系人那里获取号码,因此:

    // Define the fields that the query will return
    String[] PROJECTION = new String[] {
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };

    ContentResolver cr = getContentResolver();

    // Execute the query and receive the cursor with the results
    Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, 
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE ?", 
                    new String[] { "Name of the Contact" },
                    null );

将“联系人姓名”更改为您的联系人姓名...

现在您可以从游标中获取值

// First discover the index of the desired field (Number)
final int indexNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(indexNumber);

然后你可以使用你的意图。

PS:该示例使用 LIKE 运算符,因此您可以在游标中找到多个结果。 适应你的需要。

暂无
暂无

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

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