[BUG] All SMS messages have a person ID of -1

NeoTechni

Well-Known Member
Licensed User
Longtime User
For some reason, SmsMessages1.GetByPersonId(PersonID) wasn't working so I swapped it for SmsMessages1.GetAll to test and I got all my messages with the PersonID of -1

Id=1213, ThreadId=2, PersonId=-1, Date=1350767479791, Read=false, Type=1, Body=XXXX, Address=XXXX

I would prefer not to have to parse the address myself as it's slower than a query, but is it just my device doing it?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tried this code and for some messages PersonId is not -1:
B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim sm As SmsMessages
Dim messages As List = sm.GetAllSince(DateTime.Now - DateTime.TicksPerDay * 100)
For Each m As Sms In messages
   Log(m.PersonId)
Next
End Sub
The id is returned from the native content provider as is.
 

isr

Member
Licensed User
Longtime User
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.
 

isr

Member
Licensed User
Longtime User
Thank you, Erel.

I second NeoTechni's request to make the SMS sender/receiver's name more easily available (along the lines of CachedName in CallItem) in a future version of B4A.
 

mlc

Active Member
Licensed User
Longtime User
You can make a small library that will return the name :

B4X:
   public String GetContactNamebyPhone(String phonenumber){
      return GetContactbyPhone(phonenumber);
   }
   private String GetContactbyPhone(String phonenumber){
      String Name = "";
      Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumber));
      ContentResolver cr = BA.applicationContext.getContentResolver();
      Cursor cur = cr.query(uri, null, null, null, null);
      if(cur.moveToFirst()){
          Name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));          
      }
      return Name;
   }

to use it :

B4X:
Dim SmsMessages1 As SmsMessages 
Dim List1 As List
Dim mlc As mlccontacts *' this is the library

List1 = SmsMessages1.GetAllSince(DateTime.Add(DateTime.Now , 0, 0, -1))
List1.SortType ("Date", True)
   For i = 0 To List1.Size - 1
      Dim Sms1 As Sms
      Sms1 = List1.Get(i)
      Log(Sms1)
      Log(Sms1.Address)
            
      Dim name As String = mlc.GetContactNamebyPhone(sms1.Address)
      Log(name)
      
      Log("---------------------------------------------------------")

   Next

Obtaining the name is very fast

I hope help
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
I already have, and posted it somewhere.

However I want the bug fixed cause that would be much faster
 

isr

Member
Licensed User
Longtime User
Thank you, mlc! This seems like a great contribution.

I've already coded my more lengthy database approach (tests fine), but the next time I need to perform this task I will try your library and code.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Oh its a library, assumed wrong.

I dont know how to make libraries...
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Could you also get it to return the contact id?
Its more useful than the name
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm having problems making this into a library


BA cannot be resolved
ContactsContract cannot be resolved to a variable
ContentResolver cannot be resolved to a type
Cursor cannot be resolved to a type
PhoneLookup cannot be resolved to a variable
Uri cannot be resolved
Uri cannot be resolved to a type

I managed to fix Uri, cursor, content resolver and BA
But these 2 don't work

import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
 
Last edited:

mlc

Active Member
Licensed User
Longtime User
NeoTechni, excuse me but I have not received any notification and did not know that there was more questions.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
NeoTechni, excuse me but I have not received any notification and did not know that there was more questions.

No, that comment wasnt directed at you.
But all the other times I looked for an answer online, the person who asked found the answer but never posted it
 

mlc

Active Member
Licensed User
Longtime User
Yes, it's hateful when this happens

By the way, good job with your library
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Thank you. It shaved like 10 seconds off the loading of my app, so again, thank you.
 
Top