2016-11-12 164 views
0

下面是一个代码:呼叫联系人号码

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(n)); 
startActivity(intent); 

我想与联系人的名称,以取代n

回答

0

您可以将联系人姓名传递给该方法,并获得数如下图所示。

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(getNumber("ContacteName", this))); 
    startActivity(intent); 

getNumber是方法params是ContactName,Context。

public String getNumber(String name,Context context) 
{ 
    String number=""; 
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER}; 

Cursor people = context.getContentResolver().query(uri, projection, null, null, null); 

int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

people.moveToFirst(); 
do { 
    String Name = people.getString(indexName); 
    String Number = people.getString(indexNumber); 
    if(Name.equalsIgnoreCase(name)){return Number.replace("-", "");} 
    // Do work... 
} while (people.moveToNext()); 


if(!number.equalsIgnoreCase("")){return number.replace("-", "");} 
else return number; 
} 

另一种方式,你可以使用这样的事情来提示用户选择联系人,然后拨打该电话号码(如果有不止一个)......比传递到意向做电话:

Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
startActivityForResult(intent, 1); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
    Uri contactData = data.getData();     
    String theID = contactData.toString()); 

    //MAKE YOUR CALL .. do whatever... example: 
    ContentResolver contentResolver = getContentResolver(); 
    Uri contactData = Uri.parse(theID); 
    Cursor cur = contentResolver.query(contactData, null, null, null, null); 
    String theNumber = cur.getString(cur.getColumnIndex("data4")); 
    cur.close(); 

    Intent my_callIntent = new Intent(Intent.ACTION_CALL); 
    my_callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    my_callIntent.setData(Uri.parse("tel:" + theNumber)); 
    startActivity(my_callIntent); 


    }     

} 
+0

谢谢。我测试,我回来给你 – prince47

+0

对不起,但它不工作。模拟器返回错误 – prince47

+0

它的工作原理!谢谢 – prince47