Reputation: 128
following is my code to read contact phone number from call log and i am calling the getContactID method to get contact id by passing phone number. That method must return ID of the contact which is saved in phone book.
public void getCallDetails()
{
@SuppressWarnings("deprecation")
String sortOrder = String.format("%s limit 100 ", CallLog.Calls.DATE + " DESC");
Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI, null, null, null, sortOrder);
int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
while (managedCursor.moveToNext())
{
phoneNumber = managedCursor.getString(number);
callType = managedCursor.getString(type);
callDate = managedCursor.getString(date);
contactName = getContactname(phoneNumber);
/**
* Hrer i am calling getContactID method to get contact id by passing phone number
*/
contactId = getContactId(phoneNumber);
//callDateTime = new Date(Long.valueOf(callDate));
long seconds=Long.parseLong(callDate);
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
callDateTime = format1.format(new Date(seconds));
callDuration = managedCursor.getString(duration);
String cType = null;
int cTypeCode = Integer.parseInt(callType);
switch(cTypeCode){
case CallLog.Calls.OUTGOING_TYPE:
cType = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
cType= "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
cType = "MISSED";
break;
}
CallData calldata=new CallData(cType, phoneNumber, contactName, callDateTime, callDuration);
list.add(calldata);
}
// managedCursor.close();
}
/**
* This method gives the contact id of the saved contact
* @param phoneNumber2
* @return contact id
*/
private int getContactId(String phoneNumber2) {
// TODO Auto-generated method stub
return null;
}
/**
* this method is used to get the contact name by its phone number
* @param phoneNumber2
* @return contact name
*/
private String getContactname(String phoneNumber2) {
// TODO Auto-generated method stub
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
And i dont know how to get contact ID from its phone number, any one help me!
Upvotes: 1
Views: 2349
Reputation: 3121
/**
* Gets a list of contact ids that is pointed at the passed contact number
* parameter
*
* @param contactNo
* contact number whose contact Id is requested (no special chars)
* @param cxt
* application context
* @return String representation of a list of contact ids pointing to the
* contact in this format 'ID1','ID2','34','65','12','17'...
*/
public static String getContactRowIDLookupList(String contactNo, Context cxt) {
String contactNumber = Uri.encode(contactNo);
String contactIdList = new String();
if (contactNumber != null) {
Cursor contactLookupCursor = cxt.getContentResolver().query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(contactNumber)),
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
null, null, null);
if (contactLookupCursor != null) {
while (contactLookupCursor.moveToNext()) {
int phoneContactID = contactLookupCursor
.getInt(contactLookupCursor
.getColumnIndexOrThrow(PhoneLookup._ID));
if (phoneContactID > 0) {
contactIdList += "'" + phoneContactID + "',";
}
}
if (contactIdList.endsWith(",")) {
contactIdList = contactIdList.substring(0,
contactIdList.length() - 1);
}
}
contactLookupCursor.close();
}
return contactIdList;
}
If the phonebook has more than one contact saved with the same contact number, this method returns the id list. All you need to do is use this code and make sure the contact number you are passing to this method is digits only like "14085555555" not like "+1-408(555)-55-55" (no special chars)
Upvotes: 2