Android Question Receive sms in a service problem

mirekk36

Member
Licensed User
Longtime User
I have service that receive well all sms messages and send them via UDP to PC. Of course if service is working in backgroud'

I have added permission to receive SMS and register my service in MANIFEST

and now there is small problem because if I close my application definitely ( Exitapplication ) then when new SMS is incomning, android try start my service and CRASH, application were closed

so my question is how to prevent such situation ? Can I dynamically register my service into manifest and automaticaliy unregister just before Exitapplication ?
 

KMatle

Expert
Licensed User
Longtime User
and CRASH

I assume you work with the SMS intent filter. So it's as easy as that. You service will be called. Very easy. Nothing else to do. Must be somthing with your code (e.g. a List isn't intiaized or so). Please post the logs of the crash otherwise we're not able to help.
 
Upvote 0

mirekk36

Member
Licensed User
Longtime User
thx for your reply, but please look on my code - of my service, buft first - what I have add int manifest

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

as you se my service is named: mkSMSService

and here code of this service

B4X:
#Region  Service Attributes
    #StartAtBoot: False
  
#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)

End Sub

Sub Service_Create
  
End Sub

Sub Service_Start (StartingIntent As Intent)

    If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
      
    Dim messages() As Message
    messages = ParseSmsIntent(StartingIntent)
  
    If messages.Length > 0 Then

    ' ..... here should be my action
      
    End If

End Sub

Sub Service_Destroy

End Sub


'Parses an SMS intent and returns an array of messages
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

it work well if Main activity is paused or not. But when I do "Exitapplication" in Main Activity, then after receiving SMS this simple code of my service have some crash - because I become info "Application was closed"

(small question - how can I save log of that crach into file maybe? I cant debug it after exitapplication, can I ?)
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Put a release build on the device, run the app & get it to crash. Then connect the device via USB & click on the "Connect" button in B4A. You should see the crash in the logs view. Copy & paste that here.

- Colin.
 
Upvote 0

mirekk36

Member
Licensed User
Longtime User
Put a release build on the device, run the app & get it to crash. Then connect the device via USB & click on the "Connect" button in B4A. You should see the crash in the logs view. Copy & paste that here.

- Colin.
MANY thanks! ;) .... thanks your advice I have found BUG in my code, that was notify not initialized when my SMSService started as first

now all work ok
 
Upvote 0
Top