2011-11-03 24 views

回答

5

爲了獲取最近的通話列表,你可以在android中使用CallLog。 Here是一個很好的教程。 This也有幫助。

您可以使用它像這樣所有呼出電話:

Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, android.provider.CallLog.Calls.TYPE+"="+android.provider.CallLog.Calls.OUTGOING_TYPE, null,null); 

對於所有類型的呼叫,使用它像:

Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, null, null,null); 
+0

你好Hiral,我使用的是相同的查詢來獲取所有類型的通話記錄,但我有一個小問題,當談到雙卡設備。我的代碼在雙SIM卡設備上無法正常工作。你能幫我嗎? – Scorpion

6

一些額外的,有用的代碼:

getFavoriteContacts:

Map getFavoriteContacts(){ 
Map contactMap = new HashMap(); 

Uri queryUri = ContactsContract.Contacts.CONTENT_URI; 

String[] projection = new String[] { 
     ContactsContract.Contacts._ID, 
     ContactsContract.Contacts.DISPLAY_NAME, 
     ContactsContract.Contacts.STARRED}; 

String selection =ContactsContract.Contacts.STARRED + "='1'"; 

Cursor cursor = getContentResolver().query(queryUri, projection, selection,null,null); 


while (cursor.moveToNext()) { 
    String contactID = cursor.getString(cursor 
      .getColumnIndex(ContactsContract.Contacts._ID)); 

    Intent intent = new Intent(Intent.ACTION_VIEW); 
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID)); 
    intent.setData(uri); 
    String intentUriString = intent.toUri(0); 

    String title = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); 

    contactMap.put(title,intentUriString); 

} 
cursor.close(); 
return contactMap; 
} 

getRecentContacts:

Map getRecentContacts(){ 
Map contactMap = new HashMap(); 

Uri queryUri = android.provider.CallLog.Calls.CONTENT_URI; 

String[] projection = new String[] { 
     ContactsContract.Contacts._ID, 
     CallLog.Calls._ID, 
     CallLog.Calls.NUMBER, 
     CallLog.Calls.CACHED_NAME, 
     CallLog.Calls.DATE}; 

String sortOrder = String.format("%s limit 500 ", CallLog.Calls.DATE + " DESC"); 


Cursor cursor = getContentResolver().query(queryUri, projection, null,null,sortOrder); 


while (cursor.moveToNext()) { 
    String phoneNumber = cursor.getString(cursor 
      .getColumnIndex(CallLog.Calls.NUMBER)); 

    String title = (cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME))); 

    if(phoneNumber==null||title==null)continue; 

    String uri = "tel:" + phoneNumber ; 
    Intent intent = new Intent(Intent.ACTION_CALL); 
    intent.setData(Uri.parse(uri)); 
    String intentUriString = intent.toUri(0); 

    contactMap.put(title,intentUriString); 

} 
cursor.close(); 
return contactMap; 
} 
相關問題