2011-11-03 8 views

risposta

5

Per ottenere l'elenco delle chiamate recenti, è possibile utilizzare CallLog in Android. Here è un buon tutorial. This è anche utile.

Si può usare per tutte le chiamate in uscita come questo:

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

Per tutti i tipi di chiamate, utilizzarlo come:

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

Ciao Hiral, sto usando la stessa query per ottenere tutti i tipi di registro chiamate, ma io sto avendo un piccolo problema quando si tratta di dispositivi dual sim. Il mio codice non funziona correttamente per i dispositivi dual sim. Puoi per favore gentilmente aiutarmi in questo. – Scorpion

6

Con un po ', codice utile in più:

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; 
} 
Problemi correlati