Android Code Snippet Sending SMS with _SmsSentStatus and _SmsDelivered events

My final result for SMS sending under Android8.

B4X:
'Manifest editor
AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.READ_PHONE_STATE)

B4X:
'Starter service
Sub Process_Globals
Dim rp As RuntimePermissions
End Sub

B4X:
'Main Activity
Sub Process_Globals
    Dim Permissions As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Permissions.Initialize
    Permissions.Add(Starter.rp.PERMISSION_SEND_SMS)
    Permissions.Add(Starter.rp.PERMISSION_READ_PHONE_STATE)
End Sub

Sub Activity_Resume
    For Each permission As String In Permissions
        If Starter.rp.Check(permission) = False Then
            Sleep(200)
            Starter.rp.CheckAndRequest(permission)
            Wait For Activity_PermissionResult (permission As String, Result As Boolean)
            Log(permission)
            If Result = False Then
                ToastMessageShow("No permission " & permission, True)
                Starter.rp.CheckAndRequest(permission)
                Sleep(200)
                Return
            End If
        End If
    Next
    Activity_AfterPermission
End Sub

B4X:
'Sending SMS

Sub SendLargeSms(Destination As String, Message As String, Extra As String)
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim smsManager As JavaObject
    smsManager = smsManager.InitializeStatic("android.telephony.SmsManager").RunMethod("getDefault", Null)
    Dim parts As JavaObject = smsManager.RunMethod("divideMessage", Array As String (Message))
    Dim size As Int = parts.RunMethod("size", Null)
 
    Dim i As Intent
    i.Initialize("b4a.smssent", "")
    i.PutExtra("phone", Destination)
    i.PutExtra("message_id", Extra)
    Dim pi As JavaObject
    pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getBroadcast", _
     Array(ctxt, 0, i, 134217728))
 
    Dim i2 As Intent
    i2.Initialize("b4a.smsdelivered", "")
    i2.PutExtra("phone", Destination)
    i2.PutExtra("message_id", Extra)
    Dim pi2 As JavaObject
    pi2 = pi2.InitializeStatic("android.app.PendingIntent").RunMethod("getBroadcast", _
     Array(ctxt, 0, i2, 134217728))
 
    Dim al, al2 As JavaObject
    al.InitializeNewInstance("java.util.ArrayList", Null)
    al2.InitializeNewInstance("java.util.ArrayList", Null)
    For ii = 0 To size - 2
        al.RunMethod("add", Array(Null))
        al2.RunMethod("add", Array(Null))
    Next
    al.RunMethod("add", Array(pi))
    al2.RunMethod("add", Array(pi2))
    smsManager.RunMethod("sendMultipartTextMessage", Array(Destination, Null, parts, al, al2))
End Sub

B4X:
'initialize events for SMS statuses at needed Activity or Service
Dim phs As PhoneEvents
phs.Initialize("phs")

B4X:
'Events
Sub phs_SmsDelivered (PhoneNumber As String, Intent As Intent)
    Log("phs_SmsDelivered: PhoneNumber=" & PhoneNumber & ", message_id=" & Intent.GetExtra("message_id"))
End Sub

Sub phs_SmsSentStatus (Success As Boolean, ErrorMessage As String, PhoneNumber As String, Intent As Intent)
    Log("phs_SmsSentStatus: PhoneNumber=" & PhoneNumber & ", Success=" & Success & ", message_id=" & Intent.GetExtra("message_id"))
End Sub

B4X:
'Result

phs_SmsSentStatus: PhoneNumber=+79876543210, Success=true, message_id=4

phs_SmsDelivered: PhoneNumber=+79876543210, message_id=4
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
I have a few questions:

1) Can this be used to send an MMS (SMS with a picture/file)?

2) Does the B4A app (incorporating this code) need to be assigned by the user as the "default messaging" app before this B4A app can send an SMS?

3) Is this just for android 8 or which other versions should this work on?
 

colboy

Member
Licensed User
Longtime User
Thanks for the code, I've been trying to do this for a little bit. I have one question, I've added the two lines to the manifest, but every time I send an SMS, my phone asks me if I want to Deny, Or Allow (Once)? If I allow, it sends fine, but it asks every time. Any ideas how I can stop this?
 

colboy

Member
Licensed User
Longtime User
Thanks for the code, I've been trying to do this for a little bit. I have one question, I've added the two lines to the manifest, but every time I send an SMS, my phone asks me if I want to Deny, Or Allow (Once)? If I allow, it sends fine, but it asks every time. Any ideas how I can stop this?
So I can fix the problem by going into Installed Applications and change the Send SMS message option from Notify to Accept. I though that's what the permissions in the Manifest was meant to do? Would be good to not have the user have to do this.
 

peacemaker

Expert
Licensed User
Longtime User
change the Send SMS message option from Notify to Accept
I guess, it's your OS extra security feature, what is your OS, device name ?
Without permissions in the manifest the app could not send at all for sure.
 

colboy

Member
Licensed User
Longtime User
I guess, it's your OS extra security feature, what is your OS, device name ?
Without permissions in the manifest the app could not send at all for sure.
It's a Xiamo Redmi Note 2, with Android 5.0.2 and MIUI Global 9.6. I'm writing the application for my kids, who have Motorola G phones and a Vernee Thor. I'll report back, once I get to testing on their phones.
 

ALBRECHT

Active Member
Licensed User
Thanks Pacemaker for that code, but
Just a comment please :

Which sub would be called by : Activity_AfterPermission ? (your code at first post )
 

ALBRECHT

Active Member
Licensed User
Ok, i ve solved by :

B4X:
Permissions.Initialize
    Permissions.Add(Starter.rp.PERMISSION_SEND_SMS)
    
    For Each permission As String In Permissions
        If Starter.rp.Check(permission) = False Then
            Sleep(200)
            Log(permission & " query")
            Starter.rp.CheckAndRequest(permission)
            Wait For Activity_PermissionResult (permission As String, Result As Boolean)
            If Result = False Then
                Log(permission & " refused")
            Else
                Log(permission & " allowed")
                '.... send message
            End If
        End If
    Next

thank you for putting me on the way
 

peacemaker

Expert
Licensed User
Longtime User
Activity_AfterPermission is the sub with final init code of Activity_Resume that should be called just after getting the permissions, if you need it.
 

peacemaker

Expert
Licensed User
Longtime User
what is extra value in this codes?

What codes ? Just ignore (delete) " Activity_AfterPermission " if you do not need any other initializations after permissions are granted.
 

sagmill

Member
What codes ? Just ignore (delete) " Activity_AfterPermission " if you do not need any other initializations after permissions are granted.
great,
thankyou peacemaker,
I was able to work it, Although I don't know what "Extra" is and what it does;).
but I have a question,
Is there a way to prevent to display SMS on Android itself app?
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
I mean in SendLargeSms(Destination As String, Message As String, Extra As String) sub.

Phone number to, text of SMS, any text ID to recognize this message in confirmation events.
Just try to understand the source code it its work result.
 
Top