Android Question [SOLVED] - Responding to an incoming SMS message

rleiman

Well-Known Member
Licensed User
Greetings,

In my app I'm using PhoneEvents and PhoneStateChanged to listen and respond to incoming phone calls and send a SMS message when the phone rings. Can you tell me how to listen for an incoming SMS message? Does PhoneEvents have something I can use to do that?

I already added RECEIVE_SMS and coding to set up that permission. I just need to be able to respond to an incoming SMS.

Thanks.
 

rleiman

Well-Known Member
Licensed User
It's not as easy as it used to be. Did you search the forum? There's quite a bit of information (old & newer) about receiving & sending texts. You could start here -> https://www.b4x.com/android/forum/t...og-permissions-are-no-longer-available.98100/

- Colin.
Thanks for the link. I read something in that link about SmsInterceptor and that did the trick. I'm also posting all the coding so everyone can know what I did. It detects a phone call and now an incoming text and displays that to the logs. The app also allows you to send a text. It's simple with no error checking. Just enough to let me know I can know develop the app I want to use that functionality on.

From the manifest editor:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.RECEIVE_SMS)
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.READ_CALL_LOG)

From the main module:
#Region  Project Attributes
    #ApplicationLabel: SMS Texting
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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 Permissions As List
    Private EditTextPhoneNumber As EditText
    Private EditTextText As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")

    Permissions.Initialize
    Permissions.Add(Starter.rp.PERMISSION_SEND_SMS)
    Permissions.Add(Starter.rp.PERMISSION_RECEIVE_SMS)
    Permissions.Add(Starter.rp.PERMISSION_READ_PHONE_STATE)
    Permissions.Add(Starter.rp.PERMISSION_READ_CALL_LOG)
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
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

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

Sub ButtonSendText_Click
    SendLargeSms(EditTextPhoneNumber.Text, EditTextText.Text, "")
End Sub

Sub ButtonClose_Click
   
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "")
    i.AddCategory("android.intent.category.HOME")
    i.Flags = 0x10000000
    StartActivity(i)
End Sub

In the starter service:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim rp As RuntimePermissions
    Private PE As PhoneEvents
    Private SmsIncoming As SmsInterceptor
    Private PhoneId As PhoneId

End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.

    PE.InitializeWithPhoneState("PE",PhoneId)
    SmsIncoming.Initialize("SmsInterceptor")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)

        Log("Phone state: " & State)
       
        If State = "RINGING" Then
            Log("The incomming number is: " & IncomingNumber)
        Else
        End If
End Sub

Sub SmsInterceptor_MessageReceived (From As String, Body As String) As Boolean

    Log("MessageReceived: From = " & From & ", Body = " & Body)

    Return True
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Hi
Here it is worth adding that you won't place an application with this solution in the google store.
When the app is finished, it's going to the Amazon store. I hope they don't have the same restrictions as Google has.
 
Upvote 0
Top