Android Question RECEIVE SMS - problem = shut down app

petr4ppc

Well-Known Member
Licensed User
Longtime User
Please can I get advice?
I am trying receive SMS and I want this SMS write to Label
I am using this code - I find code on forum
http://www.b4x.com/android/forum/threads/receiving-sms.31210/#content

Because in example was not CallSubDelayed2, I add CallSubDelayed2 in service module

I add to manifesteditor:

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

serviceforsms is name of my service module, is this correct please?
I have trouble, that when I receive SMS the program is shut down and I must reopen the program. Please, what I am doing wrong?
Thank you very much
p4ppc

Manifest EDITOR:

B4X:
'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: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)

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

'End of default text.


Service: name of serice=serviceforsms
B4X:
Sub Service_Destroy

End Sub

'Service module
Sub Process_Globals
  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)
  For i = 0 To messages.Length - 1
  Log(messages(i))
  Next
  End If
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
  CallSubDelayed2(Main, "subsms", messages(i).Address & "-" & messages(i).Body)
  End If
  Return messages
End Sub



SUB IN MAIN MODULE for catching SMS:
B4X:
Sub subsms(Result AsString)
Label1.Text=Result
End sub
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel

1. I send SMS to the mobile phone (S.Galaxy s3 mini)
2. Before receiving sms the APP is shutt down with message (MyApp is STOPED)
3. SMS is received with standard "phone" style

in Logs is this:

ignoring event: badmin_devicefound
--------- beginning of /dev/log/main
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:anywheresoftware.b4a.samples.bluetooth
ignoring event: badmin_discoverystarted
ignoring event: badmin_devicefound
ignoring event: badmin_devicefound
ignoring event: badmin_discoveryfinished
** Activity (main) Resume **
Installing file.
GC_CONCURRENT freed 412K, 18% free 9714K/11783K, paused 13ms+2ms, total 43ms
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:anywheresoftware.b4a.samples.bluetooth
ignoring event: badmin_discoverystarted
ignoring event: badmin_devicefound
ignoring event: badmin_devicefound
ignoring event: badmin_discoveryfinished
** Activity (main) Resume **
Installing file.
GC_CONCURRENT freed 397K, 18% free 9715K/11783K, paused 13ms+3ms, total 56ms
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:anywheresoftware.b4a.samples.bluetooth
ignoring event: badmin_discoverystarted
ignoring event: badmin_devicefound
ignoring event: badmin_devicefound
ignoring event: badmin_discoveryfinished
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:anywheresoftware.b4a.samples.bluetooth
ignoring event: badmin_discoverystarted
ignoring event: badmin_devicefound
ignoring event: badmin_devicefound
ignoring event: badmin_discoveryfinished
** Activity (main) Resume **
GC_CONCURRENT freed 394K, 18% free 9708K/11783K, paused 14ms+2ms, total 42ms
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:anywheresoftware.b4a.samples.bluetooth
ignoring event: badmin_discoverystarted
ignoring event: badmin_devicefound
ignoring event: badmin_devicefound
ignoring event: badmin_discoveryfinished
** Activity (main) Resume **
GC_CONCURRENT freed 384K, 18% free 9708K/11783K, paused 18ms+2ms, total 58ms


Please very much, I need help,
Best regards
Thank you very much

p4ppc
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel,

when I am using basic4andorid with Tablet using B4A Bridge (wifi) , then I see logs,
when I connect my phone S.Galaxy s3 mini with same style, I dont see logs...

I realy have only infromations which I sent in my post....
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
I suspect the error is coming from here:
B4X:
CallSubDelayed2(Main, "subsms", messages(i).Address & "-" & messages(i).Body)

I am not sure but try to change to this:
B4X:
CallSubDelayed2(Main, "subsms", messages.Address & "-" & messages.Body)
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
BSNQT

thank you,
but B4A want not compile this command.

Error: Only Length is supported by arrays

Thank you
p4ppc
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel, thank you very much

I am connected now via USB and I see Logs, error is here:

** Service (serviceforsms) Start **
serviceforsms_vvvv6 (java line: 162)
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at anywheresoftware.b4a.samples.bluetooth.serviceforsms._vvvv6(serviceforsms.java:162)
at anywheresoftware.b4a.samples.bluetooth.serviceforsms._service_start(serviceforsms.java:202)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
at anywheresoftware.b4a.samples.bluetooth.serviceforsms.handleStart(serviceforsms.java:68)
at anywheresoftware.b4a.samples.bluetooth.serviceforsms.onStartCommand(serviceforsms.java:53)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2525)
at android.app.ActivityThread.access$2000(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4856)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Unable to start service anywheresoftware.b4a.samples.bluetooth.serviceforsms@418c9180 with Intent { cmp=anywheresoftware.b4a.samples.bluetooth/.serviceforsms (has extras) }: java.lang.RuntimeException: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2542)
at android.app.ActivityThread.access$2000(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4856)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
at anywheresoftware.b4a.samples.bluetooth.serviceforsms.handleStart(serviceforsms.java:68)
at anywheresoftware.b4a.samples.bluetooth.serviceforsms.onStartCommand(serviceforsms.java:53)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2525)
... 10 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at anywheresoftware.b4a.samples.bluetooth.serviceforsms._vvvv6(serviceforsms.java:162)
at anywheresoftware.b4a.samples.bluetooth.serviceforsms._service_start(serviceforsms.java:202)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
... 14 more
** Activity (main) Resume **


Please very much, can you give me advice?
Where is trouble?
thank you very much
Best regards
p4ppc
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
The trouble is SOLVED, the solution is:

B4X:
CallSubDelayed2(Main, "subsms", messages(0).Address & "-" & messages(0).Body)

Thank to all friends,
p4ppc
 
Upvote 0
Top