Sent SMS messages won't appear in the log

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm using

B4X:
Sub SendTextMessage(PhoneNumber As String, Message As String)As Boolean 
   Dim SmsManager As PhoneSms ,r As Reflector, parts As Object
   If PhoneNumber.Length>0 Then
      Try
         If Message.Length <= 160 Then 
            SmsManager.Send(PhoneNumber, Message)
         Else
             r.Target = r.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
             parts = r.RunMethod2("divideMessage", Message, "java.lang.String")
            r.RunMethod4("sendMultipartTextMessage", Array As Object(PhoneNumber, Null, parts, Null, Null), Array As String("java.lang.String", "java.lang.String", "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
         End If
         Return True
      Catch
      End Try
   End If
End Sub

to send SMS messages, but they don't appear in the history. Do I have to store them myself (which people won't like due to privacy concerns)?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to manually add it to the logs with this code:

B4X:
Sub AddMessageToLogs(body As String, address As String)
   Dim r As Reflector
   r.Target = r.CreateObject("android.content.ContentValues")
   r.RunMethod3("put", "address", "java.lang.String", address, "java.lang.String")
   r.RunMethod3("put", "body", "java.lang.String", body, "java.lang.String")
   Dim contentValues As Object = r.Target
   r.Target = r.GetContext
   r.Target = r.RunMethod("getContentResolver")
   r.RunMethod4("insert", Array As Object( _
      r.RunStaticMethod("android.net.Uri", "parse", Array As Object("content://sms/sent"), _
         Array As String("java.lang.String")), _
      contentValues), Array As String("android.net.Uri", "android.content.ContentValues"))
End Sub

You should also add the following permissions:
B4X:
AddPermission(android.permission.WRITE_SMS)
AddPermission(android.permission.READ_SMS)
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Thank you.

Questions:

For messages longer than 160 characters, I split it up into an array (parts), do I have to add each cell/part of that individually or can I just add the big/total message?

For MMS messages (with a picture) how do I add the picture to the log?

How do I set the status of the message? Ie: Sent, failed to send, draft?

Also, how can I mark an SMS thread as read?
 
Last edited:
Upvote 0

jalle007

Active Member
Licensed User
Longtime User
You will need to manually add it to the logs with this code:

B4X:
Sub AddMessageToLogs(body As String, address As String)
   Dim r As Reflector
   r.Target = r.CreateObject("android.content.ContentValues")
   r.RunMethod3("put", "address", "java.lang.String", address, "java.lang.String")
   r.RunMethod3("put", "body", "java.lang.String", body, "java.lang.String")
   Dim contentValues As Object = r.Target
   r.Target = r.GetContext
   r.Target = r.RunMethod("getContentResolver")
   r.RunMethod4("insert", Array As Object( _
      r.RunStaticMethod("android.net.Uri", "parse", Array As Object("content://sms/sent"), _
         Array As String("java.lang.String")), _
      contentValues), Array As String("android.net.Uri", "android.content.ContentValues"))
End Sub

You should also add the following permissions:
B4X:
AddPermission(android.permission.WRITE_SMS)
AddPermission(android.permission.READ_SMS)



This one is working like a charm. THX Erel
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
just to save searching (took me a while)... see Erels code Post #4 - manually write 'Sent' SMS to Logs.

A line change is required to manually write 'Received' SMS to logs ..
B4X:
Sub AddMessageToLogs(From As String, Body As String)
     Dim r As Reflector
     r.Target = r.CreateObject("android.content.ContentValues")
     r.RunMethod3("put", "address", "java.lang.String", From, "java.lang.String")
     r.RunMethod3("put", "body", "java.lang.String", Body, "java.lang.String")
     Dim ContentValues As Object = r.Target
     r.Target = r.GetContext
     r.Target = r.RunMethod("getContentResolver")
     r.RunMethod4("insert", Array As Object( _
  r.RunStaticMethod("android.net.Uri", "parse", Array As Object("content://sms/inbox"), _  '  @@ !!  ....  ("content://sms/sent")  to write sent SMS to Logs
  Array As String("java.lang.String")),ContentValues), Array As String("android.net.Uri", "android.content.ContentValues"))
    
End Sub
 
Upvote 0

Mark Zraik

Member
Licensed User
Longtime User
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
Hi, this routine was so useful for me! thanks to all you guys (speccially Erel) to help new users like me
I call this routine after another routine (SentLargeSms) and also after PhoneSMS.Send
But, sometimes the order of the log is wrong (I can see first the sms send and later the sms received on Handcent)
How can I do to that? I've tried with this without luck:
B4X:
CallSubDelayed3(Me,"SendLargeSms",ContactNumber,Text)
SendLargeSms(ContactNumber,Text)
miPhoneSMS.Send(ContactNumber,Text)
 
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
don't worry about the code Erel, it was for testing purpose.. your explanation helped to me with this.. thanks again!
 
Upvote 0
Top