【问题标题】:Call a contact in android在android中呼叫联系人
【发布时间】:2015-11-19 22:23:37
【问题描述】:

我编写了一个使用语音识别并将语音保存到字符串中的 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) 这样的东西。我如何从联系人中检索电话号码使用名称?谢谢。

【问题讨论】:

    标签: android call contact


    【解决方案1】:

    你需要从只知道名字的联系人那里获取号码,所以:

        // 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 运算符,因此您可以在游标中找到多个结果。适应你的需要。

    【讨论】:

    • 谢谢,但我收到此错误:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shahin.test/com.example.shahin.test.call}: android.database .CursorIndexOutOfBoundsException: 请求索引 -1,大小为 1
    • 尝试检查游标是否返回有效值。在创建游标后添加: if (cursor != null) { if (cursor.moveToFirst()) { // 首先发现所需字段的索引 (Number) final int indexNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone 。数字);字符串编号 = cursor.getString(indexNumber);如果该值不存在,则光标为空,可能会出现此错误。
    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多