Android Code Snippet SMS verification without permission (Android 8+)

Details - see
https://code.tutsplus.com/tutorials/android-o-phone-number-verification-with-sms-token--cms-29141

I attached relatively large sample. You can check it in emulator like described in the article.
Press button, copy token (in the log) to SMS message, send SMS.

Most of code (receiving SMS) was written by Erel.
"GetAppSpecificSmsToken"'s code is following

B4X:
Dim javaobjectInstance As JavaObject   
    javaobjectInstance.InitializeContext   
    Dim stringSMSToken As String = javaobjectInstance.RunMethod ("GetAppSpecificSmsToken", Array (Rnd (10000000, 99999999)))

#If Java
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.provider.Telephony;
    import android.telephony.SmsManager;

    public String GetAppSpecificSmsToken (int intPrivateCode) {
        SmsManager mgr = SmsManager.getDefault ();
        return mgr.createAppSpecificSmsToken (buildPendingIntent (intPrivateCode));    }
    private PendingIntent buildPendingIntent (int intPrivateCode) {
        return (PendingIntent.getActivity (this, intPrivateCode, new Intent (this, main.class), 0)); }
#End If
 

Attachments

  • test.zip
    8.2 KB · Views: 558

Cristian Achim

Member
Licensed User
Hello,
It does not work for targetSdkVersion 33.
Thank you!

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.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User

Cristian Achim

Member
Licensed User
update b4a and maybe SDK too.
I have the latest version of B4A (12.80 (64 bit)) and the latest version of the SDK. The problem occurs when I try to test on the phone, a Samsung Galaxy S24+ (Android 14).
It works ok on the emulator.
Thank you!
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
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.
Change
B4X:
return (PendingIntent.getActivity (this, intPrivateCode, new Intent (this, main.class), 0)); }
to either:
B4X:
return (PendingIntent.getActivity (this, intPrivateCode, new Intent (this, main.class), 0x02000000)); } //FLAG_MUTABLE
or
B4X:
return (PendingIntent.getActivity (this, intPrivateCode, new Intent (this, main.class), 0x04000000)); } //FLAG_IMMUTABLE

Link: https://developer.android.com/reference/android/app/PendingIntent#FLAG_IMMUTABLE
 

Cristian Achim

Member
Licensed User
I did this and it worked. I was going to write on the forum. Thank you very much!

I used:
return (PendingIntent.getActivity (this, intPrivateCode, new Intent (this, main.class), PendingIntent.FLAG_MUTABLE));
 
Top