Android Question Android 9 Pie / Android sdk 28 and PhoneEvents [SOLVED]

Magma

Well-Known Member
Licensed User
Hi there...

after updating some things at my project and manifest file to target new sdk 28... PhoneEvents can't show the Incoming Phone Number... at log...

when i have target sdk: 26 works good...

heres my code:
B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #StartCommandReturnValue: android.app.Service.START_STICKY   
#End Region

Sub Process_Globals
Dim PE As PhoneEvents
End Sub
Sub Service_Create
    Service.AutomaticForegroundMode=Service.AUTOMATIC_FOREGROUND_NEVER
Service.StartForeground(1, CreateNotification("..."))

End Sub

sub  Service_Start (StartingIntent As Intent)
PE.InitializeWithPhoneState("PE",PhoneId)
end sub
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    If State = "RINGING" Then
    ph=IncomingNumber.trim
    Log(ph)
end if
end sub

at create of main require the following:
B4X:
    Dim rp As RuntimePermissions
   rp.CheckAndRequest(rp.PERMISSION_READ_PHONE_STATE)
   wait for Activity_PermissionResult(permission As String, result As Boolean)
   Log(result)
   Dim rp As RuntimePermissions
   rp.CheckAndRequest(rp.PERMISSION_CALL_PHONE)
   wait for Activity_PermissionResult(permission As String, result As Boolean)
   Log(result)
   Dim rp As RuntimePermissions
   rp.CheckAndRequest(rp.PERMISSION_READ_PHONE_STATE)
   wait for Activity_PermissionResult(permission As String, result As Boolean)
   Log(result)
   Dim rp As RuntimePermissions
   rp.CheckAndRequest(rp.PERMISSION_READ_CONTACTS)
   wait for Activity_PermissionResult(permission As String, result As Boolean)
   Log(result)
   Dim rp As RuntimePermissions
   rp.CheckAndRequest("android.permission.ANSWER_PHONE_CALLS")
   wait for Activity_PermissionResult(permission As String, result As Boolean)
   Log(result)
at manifest:
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.basic4ppc.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="28"/>
<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.CALL_PHONE")
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission("android.permission.FOREGROUND_SERVICE")
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.ANSWER_PHONE_CALLS")
AddApplicationText(<activity android:name="anywheresoftware.b4a.objects.preferenceactivity"/>)
'End of default text.



may be something with permissions...
 

Magma

Well-Known Member
Licensed User
You need to add this permission to the manifest editor:
B4X:
AddPermission(android.permission-group.CALL_LOG)
And request it at runtime.
sorry but this gives me that error at compiling:
B4X:
B4A Version: 8.80
Parsing code.    (0.02s)
Building folders structure.    (0.02s)
Compiling code.    (0.01s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.39s)
Generating R file.    Error
AndroidManifest.xml:24: Tag <uses-permission> attribute name has invalid character '-'.
 

Magma

Well-Known Member
Licensed User
Yes this is... :)

but... Something not going well...

At android 28 emulator:
For a reason... when phone rings first time... gives a 0 length incoming number... and at second ring give the whole incoming number... like having a small delay to understand the number...

One solution will be to check if length is bigger than 0... but... this will not catch the "unknown/hiddeb numbers" (0 length) - that is a problem too :-(
 

Erel

Administrator
Staff member
Licensed User
Based on your description, PhoneStateChanged must be raised several times. The IncomingNumber parameter cannot change unless the event is raised again.

It is difficult to give an answer without understanding the exact behavior.

Maybe something like this is better:
B4X:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
 RingIndex = RingIndex + 1 'provess global int
 Dim MyIndex = RingIndex
 Sleep(50)
 If MyIndex <> RingIndex Then Return
 Log("IncomingNumber: " & IncomingNumber)
End Sub
This code will ignore the frequent events and will only handle the last one.
 

OliverA

Expert
Licensed User
found a quick fix... don't know ... if it is good... your opinion ?
Did you test this on a real device? Emulators can be quirky and a workaround that makes something work on an emulator that the emulator broke in the first place may break on a real device.
 
Top