Android Question Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE

LGS

Member
Licensed User
Longtime User
Hello everyone
To send sms I was using
B4X:
Sub SendMultipartTextMessage(Number As String, Message As String, SubscriptionId As Int)  As ResumableSub
    Dim r As Reflector
    r.Target = r.RunStaticMethod("android.telephony.SmsManager", "getSmsManagerForSubscriptionId", Array As Object(SubscriptionId), Array As String("java.lang.int"))
    Dim parts As Object
    parts = r.RunMethod2("divideMessage", Message, "java.lang.String")
    r.RunMethod4("sendMultipartTextMessage", _
        Array As Object(Number, Null, parts, Null, Null), _
        Array As String("java.lang.String", "java.lang.String", _
            "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
    Return True
End Sub

But this works without problem with devices with Android 10

I am currently testing with a device with Android 14 and sms are not being sent.

Then I try to use the following:
B4X:
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 pi As JavaObject
   pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getBroadcast", _
     Array(ctxt, 0, i, 134217728))
   Dim size As Int = parts.RunMethod("getSize", 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

But this error is shown in the log:
B4X:
java.lang.IllegalArgumentException: com.sss.test: 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.checkPendingIntent(PendingIntent.java:430)
    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:733)
    at android.app.PendingIntent.getBroadcast(PendingIntent.java:720)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at com.salinasystems.consultas.b4xcitas._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv6(b4xcitas.java:5442)
    at com.salinasystems.consultas.b4xcitas$ResumableSub_etiRecordatorio_Click.resume(b4xcitas.java:3965)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.keywords.Common$15.run(Common.java:1804)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:224)
    at android.os.Looper.loop(Looper.java:318)
    at android.app.ActivityThread.main(ActivityThread.java:8755)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)

I appreciate the support if someone handled this error
 
Solution
in java i use PendingIntent.FLAG_ONE_SHOT|PendingIntent.FLAG_IMMUTABLE) for the flags
so, in b4a, you bit.or() these constants:
Constant Value: 67108864 (0x04000000)
Constant Value: 1073741824 (0x40000000)
this would give you:
1140850688 (0x44000000)
instead of your:
134217728 (0x8000000)
but you could bit.or() your flag with the flag_immutable value or do whatever math yourself and plug the result in the flags parameter

i'm running android 14

drgottjr

Expert
Licensed User
Longtime User
in java i use PendingIntent.FLAG_ONE_SHOT|PendingIntent.FLAG_IMMUTABLE) for the flags
so, in b4a, you bit.or() these constants:
Constant Value: 67108864 (0x04000000)
Constant Value: 1073741824 (0x40000000)
this would give you:
1140850688 (0x44000000)
instead of your:
134217728 (0x8000000)
but you could bit.or() your flag with the flag_immutable value or do whatever math yourself and plug the result in the flags parameter

i'm running android 14
 
Upvote 0
Solution

drgottjr

Expert
Licensed User
Longtime User
do you have runtime permission send_sms?
 
Upvote 0

LGS

Member
Licensed User
Longtime User
B4X:
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.SEND_SMS)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
works for me. are there any error messages that you can share? do you follow what each statement in the sub does?
 

Attachments

  • sms.png
    sms.png
    230.3 KB · Views: 91
Upvote 0

LGS

Member
Licensed User
Longtime User
Hello drgottjr

The problem was that the telephone field was defined as numeric in the Bd and the "0" was suppressed.
And the format needed to send SMS is 0##########
Corrected this, everything is Ok

Thanks for the support
 
Upvote 0
Top