Android Question B4A SMS Class vs android SMSMessage Class

JordiCP

Expert
Licensed User
Longtime User
Hi all,

Looking through tutorials, I can easily read stored B4A SMS messages. For instance,
B4X:
    Dim SMSMsgs As SmsMessages
    Dim SMSList As List

    SMSList=SMSMsgs.GetAll
    For each mySMS as SMS in SMSList
       ....
    Next

Now, for a given SMS, I would like to get the raw SMS info ( as getPDU() method in SMSMessageClass does). Can I obtain it using reflection or JavaObjects from a B4A SMS object, or do I need to use some other approach?

In other words, is the B4A SMS object directly related to Android SMSMessage so that I can access it and its methods?
 

JordiCP

Expert
Licensed User
Longtime User
Thanks Erel :)

So I guess that the raw format is lost when an incoming SMS is stored and only the available B4A SMS object fields are the ones that are kept?


--EDIT--

Just found THIS, which is the answer to my latest question ;)
 
Last edited:
Upvote 0

atulindore

Member
Licensed User
Longtime User
Hi Team
My phone is updated with MIUI7 based on Kitkat.
My existing sms program has started giving error while reading the stored SMS .. specially in case of sms received from shortcodes like "AZ-AIRINF" .

Error : java.lang.NumberFormatException: Invalid Int: "AZ-AIRINF"
I am using simple list1 = smsMessage1.GetAll

If I remove these particular SMS from inbox , it works fine .. any suggestion / solution .
 
Upvote 0

atulindore

Member
Licensed User
Longtime User
List1 = SmsMessages1.GetAllSince(DateTime.Add(DateTime.Now, 0, 0, -15))

Error occurred on line: 164 (main)
java.lang.NumberFormatException: Invalid int: "AZ-AIRINF"
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parse(Integer.java:374)
at java.lang.Integer.parseInt(Integer.java:365)
at java.lang.Integer.parseInt(Integer.java:331)
at anywheresoftware.b4a.phone.SmsWrapper.get(SmsWrapper.java:98)
at anywheresoftware.b4a.phone.SmsWrapper.GetAllSince(SmsWrapper.java:76)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at ibm.chattool.main.afterFirstLayout(main.java:98)
at ibm.chattool.main.access$100(main.java:16)
at ibm.chattool.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
at dalvik.system.NativeStart.main(Native Method)
 
Last edited:
Upvote 0

atulindore

Member
Licensed User
Longtime User
<RESOLVED>

Thanks Erel.
I tried to use ContentResolver . It is working fine .. Sample Code as below .

B4X:
Sub GetSMS(SMSstr As String) As List
    Dim SMSList As List
    Dim Msgid,MsgDt As String
    Dim MsgFrom , MsgBody As String
    SMSList.Initialize
    Dim ur As Uri
    ur.Parse("content://sms")

    Dim crsr As Cursor = cr.Query(ur,Array As String("_id","date","address","body"),"address = ?", Array As String(SMSstr),"_id")

    Log("Number of columns = " & crsr.ColumnCount)
    Log("Number of SMS = " & crsr.RowCount)
    crsr.Position = 0
    For r = 0 To crsr.RowCount -1
        crsr.Position = r
        Msgid = crsr.GetInt("_id")
        MsgDt = crsr.GetLong("date")
        MsgFrom = crsr.GetString("address")
        MsgBody = crsr.GetString("body")
        SMSList.Add (Msgid& "!"&MsgDt&"!"&MsgFrom&"!"&MsgBody)
    Next
    crsr.Close
    Return SMSList
End Sub
 
Last edited:
Upvote 0

atulindore

Member
Licensed User
Longtime User
Attaching the complete sample code to Read SMS .. May help new learners like me ...
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type SMSType (Msgid As Int,Msgdt As String,MsgFrom As String,MsgBody As String)
    Public SMSList As List

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim cr As ContentResolver   '------
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
       SMSList.Initialize
    End If
    Activity.LoadLayout("Layout1")
    cr.Initialize("cr")


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Button1_Click
    Dim sms1 As List
    sms1.Initialize
    sms1 = GetSMS("AZ-AIRINF")
'    Log(sms1.Size)
    For i = 0 To sms1.Size -1
        Dim ms As SMSType
        ms  = sms1.Get(i)
        Log(ms.Msgid)
        Log(ms.Msgdt)
        Log(ms.MsgBody)
    Next
End Sub
Sub GetSMS(SMSstr As String) As List
    SMSList.Clear
    Dim ur As Uri
    ur.Parse("content://sms")
'   Dim crsr As Cursor = cr.Query(ur, Null, "", Null, "")  '' To read all fields in sms without any filter
    Dim crsr As Cursor = cr.Query(ur,Array As String("_id","date","address","body"),"address = ?", Array As String(SMSstr),"_id")

    Log("Number of columns = " & crsr.ColumnCount)
    Log("Number of SMS = " & crsr.RowCount)
    crsr.Position = 0
    For r = 0 To crsr.RowCount -1
        crsr.Position = r
        Dim sms1 As SMSType
        sms1.Initialize
        sms1.Msgid = crsr.GetInt("_id")
        sms1.MsgDt = crsr.GetLong("date")
        sms1.MsgFrom = crsr.GetString("address")
        sms1.MsgBody = crsr.GetString("body")
        SMSList.Add(sms1)
    Next
    crsr.Close
    Return SMSList
End Sub

[CORRECTED]
 
Last edited:
Upvote 0

atulindore

Member
Licensed User
Longtime User
How can I put date condition in below code (Cursor ) .. I want to read SMS only for last 10 days . Epoch Seconds for 10 Days : 864000

B4X:
Sub GetSMS(SMSstr As String) As List
    Dim SMSList As List
    Dim Msgid,MsgDt As String
    Dim MsgFrom , MsgBody As String
    SMSList.Initialize
    Dim ur As Uri
    ur.Parse("content://sms")

    Dim crsr As Cursor = cr.Query(ur,Array As String("_id","date","address","body"),"address = ?", Array As String(SMSstr),"_id")

    Log("Number of columns = " & crsr.ColumnCount)
    Log("Number of SMS = " & crsr.RowCount)
    crsr.Position = 0
    For r = 0 To crsr.RowCount -1
        crsr.Position = r
        Msgid = crsr.GetInt("_id")
        MsgDt = crsr.GetLong("date")
        MsgFrom = crsr.GetString("address")
        MsgBody = crsr.GetString("body")
        SMSList.Add (Msgid& "!"&MsgDt&"!"&MsgFrom&"!"&MsgBody)
    Next
    crsr.Close
    Return SMSList
End Sub
 
Upvote 0
Top