سؤال

I'm currently making an SMS viewing application and using the ContentResolver to obtain all SMS messages on the phone (Yes, I understand the risks). Like other applications, I want to group all messages from the same person to one thread, display the latest message from them, and order the contacts by date of the last message.

When it comes to the address values of the incoming messages, they all contain the country code (e.g. +44123456789). But when the user saves his contacts, he could ignore the country code and simply type in the local format. So all outgoing messages are stored as 0123456789.

So, the database will contain the same address in both formats, +44123456789 and 0123456789. How do you match this 2 and remove the duplicate address?

Note: 1) Messages from the same person may not have the same "thread id" 2) There may not be a "contact id"/"display name" value for the address

هل كانت مفيدة؟

المحلول

Actually, messages to and from the same contact are in the same thread, therefore they have the same thread_id. (Apart from multiple recipient messages, which are in their own thread).

By looking in content://sms and storing a list of obtained thread_ids you can make sure there's no duplicates. With the address value you can use the following code to obtain the Display name.

Now, I'm trying to optimise this:

private String quickCallerId(String phoneNumber){
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    ContentResolver resolver=getContentResolver();
    Cursor cur = resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
    if(cur!=null&&cur.moveToFirst()){
            String value=cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME));
            if(value!=null){ 
                cur.close();
                return value;
            }
    }
    cur.close();
    return "";
}

نصائح أخرى

I don't have the code on me, but it's pretty easy to parse a string from right to left. You could do this and simply set an arbitrary limit on how accurate it must be to stop.

For instance (pseudo-code), given 2 strings (string1 and string2):

if first-char = '+'
  len = 9
else
  len = length(string1)
end
len = min(len, length(string2))
match = true
for i = len to 1
  if substr( string2, i, 1) != substr( string2, i, 1)
    match = false
    quit
  end
  i--
end

You could get fancier by checking the characters immediately following the '+' sign to determine the country code, which would let you know how long that country's phone numbers are likely to be.

You would also need to check for people entering numbers as e.g. '(123) 456-7890 x1234' if that's a possibility. So it might be simpler to use some regexp variant...

Rory

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top