Android Question Inserting SMS into inbox or outbox

Mohamed Ghoname

Member
Licensed User
Dears I face an issue while inserting the sms into sms catalog nigher inbox nor outbox the below is the code I used


Sub InsertSMS (Number As String,Body As String)
Dim resolver As ContentResolver
resolver.Initialize("res")
Dim values As ContentValues
values.Initialize
values.PutString("address",Number)
values.PutString("body",Body)
values.PutString("read", "1")
values.PutString("subject", "subject")
values.PutInteger("date",DateTime.Now)

Dim Uri1 As Uri
Uri1.parse("content://sms/inbox")

Try
resolver.Insert(Uri1,values).ParseId
Catch
Msgbox(LastException,"")
End Try
End Sub

thank you in advance.
 

mangojack

Well-Known Member
Licensed User
please use [Code ] 'my code ... [/Code ] (without spaces) .... makes it much easier to read.

Maybe try this
B4X:
Sub AddMessageToLogs(Number As String, Body As String)
 
   'http://www.basic4ppc.com/android/forum/threads/sent-sms-messages-wont-appear-in-the-log.22939/
     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
 

Mohamed Ghoname

Member
Licensed User
thank you bro for your kind reply but its still not working and after more searching i found my app must be the default sms application starting from android 4.4 kitkat and actulally this is my current one. But I didnt tried to make my app as default sms app.

BUT THE GOOD THING
I tried it on my previous mobile which note 1 android 4.2 and both codes are working fine you code and mine, So I think the issue that is the kitkat.
 

PoleStar

Member
Licensed User
please use [Code ] 'my code ... [/Code ] (without spaces) .... makes it much easier to read.

Maybe try this
B4X:
Sub AddMessageToLogs(Number As String, Body As String)

   'http://www.basic4ppc.com/android/forum/threads/sent-sms-messages-wont-appear-in-the-log.22939/
     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
Thanks
but, how can set date and time for message? :)

i using this code but not working :
B4X:
 r.RunMethod3("put", "date", "java.lang.String",  "18/02/1992", "java.lang.String")
  r.RunMethod3("put", "time", "java.lang.String",  "11:15", "java.lang.String")
 

Erick Kalugdan

Member
Licensed User
I tried the code in post #2 and added the WRITE_SMS permission in the manifest but it doesn't work.

My phone is the Huawei GR5 with Android version 5.1.1

Am I missing anything here or has Android changed permissions since post #2 was posted?
 

mangojack

Well-Known Member
Licensed User
Top