Android Question android service that turns the screen back on and starts the app

tmtube73

Active Member
Licensed User
good morning everyone, I'm trying to develop an always active android service that receives an sms and if the sms message contains a particular string, for example "x123x" then it turns the screen back on and starts an app already installed in android.

I'm new to this language, do you think more experts can be done? or do I have to make it in java? if it can be done do you have any advice to give me?
 

tmtube73

Active Member
Licensed User
thanks for the reply, i don't need to load the app on the playstore it is for personal use, once i can intercept the sms message how can i tell adroid to turn on the display and start a certain app?

Thanks in advance
 
Upvote 0

tmtube73

Active Member
Licensed User
i am trying this example to receive sms

https://www.b4x.com/android/forum/t...rcepting-sms-messages-in-the-background.20103 / # content

I copied the permission code in the manifest file and the rest of the code in the "starter" area of the project

when I compile the project I get the following error:
s1_br module not found (manifest editor)
undeclared variable 'r' used before it was assigned a value

it seems that a reference to a reflector library is missing

can anyone tell me where am i wrong?
 
Upvote 0

tmtube73

Active Member
Licensed User
good morning everyone, I tried and tried again but nothing I can not use the example at this link to receive sms


I wish that once the app was started she was listening as a service and if she receives an sms containing the string "123" then she executes a sub that I call "commands" in which I perform 2 operations

step 1 ---> by turning the screen back on
step 2 ---> start an app called "means"
============================

B4X:
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(Starter,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

then in the service module named "Starter" I put the following code

B4X:
#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.
    Type Message (Address As String, Body As String)
    Dim Notification1 As Notification
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.
    Notification1.Initialize
    Notification1.Icon = "restart1.png" 'use the application icon file for the notification
    Notification1.Vibrate = False
End Sub

Sub Service_Start (StartingIntent As Intent)
    Notification1.SetInfo("Ricezione SMS", "Downloading: ", Main)
    Service.StartForeground(1, Notification1)
    If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
        Dim messages() As Message
        messages = ParseSmsIntent(StartingIntent)
        For i = 0 To messages.Length - 1
            Log(messages(i))
        Next
        ToastMessageShow("message received" & messages, True)
    End If
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one) 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 ParseSmsIntent (in As Intent) As Message()
    Dim messages() As Message
    If in.HasExtra("pdus") = False Then Return messages
    Dim pdus() As Object
    Dim r As Reflector
    pdus = in.GetExtra("pdus")
    If pdus.Length > 0 Then
        Dim messages(pdus.Length) As Message
        For i = 0 To pdus.Length - 1
            r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
            Array As Object(pdus(i)), Array As String("[B"))
            messages(i).Body = r.RunMethod("getMessageBody")
            messages(i).Address = r.RunMethod("getOriginatingAddress")
        Next
    End If
    Return messages
End Sub
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
HI
B4X:
Sub ParseSmsIntent (in As Intent) As Message()
    Dim messages() As Message
    If in.HasExtra("pdus") = False Then Return messages
    Dim pdus() As Object
    Dim r As Reflector
    pdus = in.GetExtra("pdus")
    If pdus.Length > 0 Then
        Dim messages(pdus.Length) As Message
        For i = 0 To pdus.Length - 1
            r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
            Array As Object(pdus(i)), Array As String("[B"))
            messages(i).Body = r.RunMethod("getMessageBody")
            messages(i).Address = r.RunMethod("getOriginatingAddress")
            
            Dim messbody as string= messages(i).Body
            if messbody="123" then
            StartActivity(Main)
            end if
            
        Next
    End If
    Return messages
End Sub
 
Upvote 0

tmtube73

Active Member
Licensed User
hello, I tried and my app starts correctly, I see the icon at the top of the status bar and it tells me that it is running, however when I send a sms containing "123" without the double quotes it doesn't open activity "Main" something not it works but i don't understand what maybe i have to authorize somewhere in android this app? or should I install it in release mode and not in debug mode?
 
Upvote 0

tmtube73

Active Member
Licensed User
I discovered the problem was the SMS authorization in android to be activated manually, now it seems to receive the sms but when I send a sms the application closes whatever the content of the message
 
Upvote 0

tmtube73

Active Member
Licensed User
before closing the app I managed to take a screenshot, this message came out at the bottom of the smartphone display:

message received [Lb4a.example.starter $
_message; @ 38739bc

and then the app closes, any advice?
 
Upvote 0

tmtube73

Active Member
Licensed User
I noticed that if I comment on these 2 lines
'Notification1.SetInfo ("SMS reception", "listening", Main)
'Service.StartForeground (1, Notification1)

the app works and when i receive sms with text "123" I open a teastmessage ("message received", True) but I don't see the icon at the top in the status bar, while if I include those 2 lines at the top removing the comment, I see the icon at the top but when I receive text messages the app closes abnormally
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
Try like that...Don't panic and paste the error log ...
B4X:
  Dim messbody as string= messages(i).Body
            log(messbody)
            if messbody="123" then
            StartActivity(Main)
          Service.StopAutomaticForeground
    end if
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…