2015-11-19 68 views
-1

我写了一个使用语音识别并将语音保存为字符串的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的实际数量,我需要通过爸爸的号码it.For例如像Uri.parse(dad.PhoneNumber)。如何我可以通过使用名称从联系人中检索电话号码吗?谢谢。

回答

0

你需要获得你知道联系人只名称中的数字,所以:

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

+0

谢谢,但我得到这个错误:java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.shahin.test/com.example.shahin.test.call}:android.database.CursorIndexOutOfBoundsException:Index - 1请求,大小为1 – shahin

+0

尝试检查游标是否返回有效值。创建游标后,只需添加: if(cursor!= null)if(cursor.moveToFirst()){0}首先发现所需字段的索引(Number) final int indexNumber = cursor.getColumnIndex ContactsContract.CommonDataKinds.Phone.NUMBER); String number = cursor.getString(indexNumber); } } 如果该值不存在,则光标将为空,并可能发生此错误。 – oleonardomachado