Android Question Send large SMS

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hi everyone,
Based on this thread https://www.b4x.com/android/forum/threads/multipart-sms-sent-status.74903/#post-483060 i try to send large SMS, but i receive the following error. Can anybody help me how to solve ?
Thank you
Logger connected to: samsung SM-A715F
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 123 (B4XMainPage)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4a.example.b4xmainpage._sendlargesms(b4xmainpage.java:231)
at b4a.example.b4xmainpage._btn_large_click(b4xmainpage.java:184)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7892)
at android.widget.TextView.performClick(TextView.java:16220)
at android.view.View.performClickInternal(View.java:7869)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:30891)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.IllegalArgumentException: b4a.example: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:408)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:688)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:675)
... 25 more
 
Solution
Thank you Erel,
It's working ! !
Below is the functional code.
Send large SMS:
Sub SendLargeSms(Destination As String, Message 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(Message))
    Dim i As Intent
    i.Initialize("b4a.smssent", "")
    Dim p As Phone 'Phone library
    Dim flags As Int = 134217728
    If p.SdkVersion >= 31 Then
        flags = Bit.Or(33554432, flags)
    End If
    Dim pi As JavaObject
    pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getBroadcast", _
     Array(ctxt, 0, i...

emexes

Expert
Licensed User
Longtime User
Log output:
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 123 (B4XMainPage)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)

It'd be useful to see what the code looks like around line: 123 (B4XMainPage)
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Lol I was so busy looking for line number clues, that I completely missed this:
Log output:
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.IllegalArgumentException: b4a.example: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:408)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:688)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:675)

Are there any references in the code to flags like that? Or any obvious place to add the flag?

At least it gives something decent to search on.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
This comment made me 🥺 but is always good to know we're not alone.
I lost a week checking my code for possible errors with the flags and there was none. And now I add this line everything works as expected. Why Google makes our life so hard some times. Did they even check their releases or in order to meet deadlines they release unchecked code? Anyway, THANK YOU very much for this. You saved me a lot of headaches! – Nick Commented Oct 21, 2021 at 6:43
https://stackoverflow.com/questions/68228666/targeting-s-version-10000-and-above-requires-that-one-of-flag-immutable-or-fl
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Is always good to know we're not alone.
Yes, this error is faced many times by a few members in the forum. Searching for the error will give us the threads.

https://www.b4x.com/android/forum/pages/results/?query=FLAG_IMMUTABLE

Maybe this post helps:
 
Last edited:
Upvote 0

StarinschiAndrei

Active Member
Licensed User
Longtime User
Thank you form your involvement , unfortunately i can't find the problem.
i attached a simply test app.
 

Attachments

  • smstest.zip
    10.2 KB · Views: 81
Upvote 0

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hi Erel,
Here is the error :

Logger connected to: samsung SM-A715F
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 44 (B4XMainPage)
java.lang.RuntimeException: Method: getSize not found in: java.util.ArrayList
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:363)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
at b4a.example.b4xmainpage._sendlargesms(b4xmainpage.java:132)
at b4a.example.b4xmainpage._btn_send_click(b4xmainpage.java:67)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7892)
at android.widget.TextView.performClick(TextView.java:16220)
at android.view.View.performClickInternal(View.java:7869)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:30891)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

Thank you
 
Upvote 0

StarinschiAndrei

Active Member
Licensed User
Longtime User
Thank you Erel,
It's working ! !
Below is the functional code.
Send large SMS:
Sub SendLargeSms(Destination As String, Message 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(Message))
    Dim i As Intent
    i.Initialize("b4a.smssent", "")
    Dim p As Phone 'Phone library
    Dim flags As Int = 134217728
    If p.SdkVersion >= 31 Then
        flags = Bit.Or(33554432, flags)
    End If
    Dim pi As JavaObject
    pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getBroadcast", _
     Array(ctxt, 0, i, flags))
    Dim size As Int = parts.RunMethod("size", Null)
    Dim al As JavaObject
    al.InitializeNewInstance("java.util.ArrayList", Null)
    For ii = 0 To size - 2
        al.RunMethod("add", Array(Null))
    Next
    al.RunMethod("add", Array(pi))
    smsManager.RunMethod("sendMultipartTextMessage", Array(Destination, Null, parts, al, Null))
End Sub
 
Upvote 0
Solution
Top