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: