2016-11-12 202 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