ThreadId from PhoneNumber

BaGRoS

Active Member
Licensed User
Longtime User
How to get ThreadId knowing only the phone number? I mean ThreadId in text messages.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use the new ContentResolver library for that:
B4X:
Sub Process_Globals
   Private cr As ContentResolver
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      cr.Initialize("cr")
   End If
   Dim smsUri As Uri
   smsUri.Parse("content://sms")
   Dim address As String = "999999"
   Dim crsr As Cursor = cr.Query(smsUri, Array As String("thread_id", "body"), "address LIKE ?", _
      Array As String("%" & address & "%"), "")
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      Dim tid As Int = crsr.GetInt("thread_id")
      Log(tid)
      Log(crsr.GetString("body"))
   Next
   crsr.Close
End Sub

Add this line to the manifest editor (not required if you are also using SmsMessages object):
B4X:
AddPermission("android.permission.READ_SMS")
 
Upvote 0
Top