Android Question sms intent not triggering

gpa

Member
Licensed User
Longtime User
Hi...
I have a simple sms send / receive test program. The receive intent is as per https://www.b4x.com/android/forum/t...cepting-sms-messages-in-the-background.20103/
It works perfectly on one phone - Samsung S8, Android 9; but the intent fails to trigger on another phone - Huawei P20.
b4a v 9.50

Any suggestions as to how to debug this situation?

Thanks!

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

service module:
#Region  Service Attributes
    #StartAtBoot: True
   
#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
    Log("hello")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Log(StartingIntent)
    ToastMessageShow(StartingIntent, True)
    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
'    Sleep(2)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
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

Sub Service_Destroy

End Sub
 
Last edited:

gpa

Member
Licensed User
Longtime User
Hi...
Thanks. Yes, not using this through play store so that's not a problem.
I didn't realise that about b4a!
I've not had an update email lately - perhaps that's why - do they still get sent out?
I'll upgrade it! [edit - Upgrade made no difference]

I have this in Main - is that adequate for runtime?

B4X:
Sub Process_Globals
    Dim MyText As PhoneSms
    Dim rp As RuntimePermissions
    Dim Permissions As List
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("SMSTest")
    Activity.Title = "SMS"
    Permissions.Initialize
    Permissions.Add(Starter.rp.PERMISSION_SEND_SMS)
    Permissions.Add(Starter.rp.PERMISSION_READ_PHONE_STATE)
    Permissions.Add(Starter.rp.PERMISSION_RECEIVE_SMS)  
    Dim phs As PhoneEvents
    phs.Initialize("phs")
End Sub
 
Upvote 0

gpa

Member
Licensed User
Longtime User
Anyone have any idea why this isn't working, or how to debug the 'intent'?
Thanks.....
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Permissions.Initialize Permissions.Add(Starter.rp.PERMISSION_SEND_SMS) Permissions.Add(Starter.rp.PERMISSION_READ_PHONE_STATE) Permissions.Add(Starter.rp.PERMISSION_RECEIVE_SMS)
this code does not do anything useful.

I suggest to watch the Runtime Permission Tutorial Video.


Also this is a suggested reading:

 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Try this:

Put this in the manifest

B4X:
AddPermission(android.permission.RECEIVE_SMS)
AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.WRITE_SMS)

add this to request permissions function and call it from activity_resume
B4X:
sub request_permissions
        Dim  rp As RuntimePermissions

        rp.CheckAndRequest(rp.PERMISSION_SEND_SMS)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            ... permission granted
        else
            ... permission not granted
        End If

       rp.CheckAndRequest(rp.RECEIVE_SMS)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            ... permission granted
        else
            ... permission not granted
        End If

end sub

regards

John.
 
Upvote 0

gpa

Member
Licensed User
Longtime User
Thanks for the comments, both....

John - those lines are already in the manifest, along with the intent definition - see above...

Both - I tried both John's suggestion and the alternate runtime permission suggested by your link Manfred.
The manifest also states android 26+

<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
(b4a default I think)

The app does bring up the phone send and receive, and sms send and view dialogues to be accepted at runtime.

The sms send works on both phones, including delivered notifications etc.

logs, view permissions looks ok:

Clipboard01.jpg


The difference is that on the Huawei, the sms receive intent does nothing. On the Samsung it works 100% every time.
Both phones are Android 9....

Is there some log kept in android that might show what causes the different 'intent' action result?

edit - this is now in activity_resume too:
For Each permission As String In Array(rp.PERMISSION_READ_PHONE_STATE, rp.PERMISSION_SEND_SMS,rp.PERMISSION_RECEIVE_SMS)
rp.CheckAndRequest(permission)
Wait For Activity_PermissionResult (permission As String, Result As Boolean)
If Result = False Then
ToastMessageShow("Permission fail", True)
Return
Else
ToastMessageShow("Permission ok", True)
End If
Next

Gets ok every time...

edit 2:
This is the log from the app on Samsung:

** Activity (main) Resume **
** Receiver (sms1) OnReceive **
*** Service (sms1) Create ***
hello
** Service (sms1) Start **
(Intent) Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x19000010 cmp=b4a.example/.sms1$sms1_BR (has extras) }
[Address=+447836350240, Body=Rty, IsInitialized=false]

That never happens on the Huawei when an sms is received.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I don't know how or if this could be the cause, but recent US laws prohibit google apps from installing (and I think running) on Huawei devices.

I don't know if this law just effects newer built devices and/or existing devices.
 
Last edited:
Upvote 0

gpa

Member
Licensed User
Longtime User
Doesn't apply to existing devices.... And, this is not installed via the store, also play protect is off! :)
I need to borrow some more phones to test!
 
Upvote 0

gpa

Member
Licensed User
Longtime User
So, tested so far:
Samsung S8 android 9: works fine
LG G4 android 7: works fine
Oukitel K10000 android 7: works fine

Huawei P20 android 9: send works, receive intent fails
Huawei P30 android 9: send works, receive intent fails

So it looks to be an issue with receive sms intent on Huawei phones.
Trouble is, there's a lot of them out there!

Any ideas, Erel, how I can debug this?
 
Upvote 0

gpa

Member
Licensed User
Longtime User
It is for a customer. It's possible that it's a Huawei issue -- but there's a lot out there, so I think it would be worth trying to find out....
 
Upvote 0

gpa

Member
Licensed User
Longtime User
No, permissions all show ok.

manifest:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(sms1,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
 
Upvote 0
Top