I have the same problem with PersonId returning -1 for every contact, including contacts that are fully described in the Contacts/People application. I have experienced this with two different phones (HTC Eris running Eclair and LG-E739 running Gingerbread) and two different carriers. I have tried both your code above, Erel, as well as somewhat different code of my own, and I still get -1 for every contact. (Of course, some of these should be -1, when the sender/receiver of the message is not in the Contacts application.)
If PersonId doesn't work reliably or at all (at least for these devices/versions of Android/mobile carriers), how can we find the name of the SMS sender/receiver? The only strategy I can think of right now is essentially to use GetAll in Contacts, insert all the Contacts into a database table, and then query the database for a match between a particular SMS sender/receiver's phone number (called "Address" in Sms), and then retrieve that contact's name (if the number appears in the Contacts table at all).
Or are there other more efficient strategies?
It seems like Phone is well developed for the call log, but not as well developed for the text log. Is there something difficult or awkward in the Android text log that explains this?
Thank you for any suggestions.