Android Question Library raised events and android nougat

Massimiliano

Member
Licensed User
Longtime User
Hello,
I'm having some problems with android 7 (I'm using b4A v7)

Events are not fired by some libraries.
Here's an example for PhoneStateListener library

'*** LIBRARY LINK ***
https://www.b4x.com/android/forum/threads/phonestatelistener.12377/

'*** PERMISSION INCLUDED IN MANIFEST ***
AddPermission("android.permission.ACCESS_COARSE_LOCATION")
AddPermission("android.permission.ACCESS_NETWORK_STATE")
AddPermission("android.permission.CHANGE_NETWORK_STATE")
AddPermission("android.permission.ACCESS_COARSE_UPDATES")
AddPermission("android.permission.READ_PHONE_STATE")

B4X:
Sub Process_Globals
End Sub

Sub Globals
    Dim psl As PhoneStateListener
End Sub

Sub Activity_Create(FirstTime As Boolean)
    psl.Initialize("PSL", True)
    psl.startListening()
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub PSL_onSignalStrengthsChanged (signalStrength As String)
    Log(signalStrength)
End Sub

*** LOGS ***
-------------------------------------------------------------------------
** Activity (main) Create, isFirst = true **
PhoneStateListener has been initialized.
** Activity (main) Resume **
onServiceStateChanged IN SERVICE
onMessageWaitingIndicatorChanged false
onCallForwardingIndicatorChanged false
onCellLocationChanged [30968,81464840,-1]
onCallStateChanged: 0 &
onDataConnectionStateChanged Connected
onDataActivity INOUT
onSignalStrengthChanged SignalStrength: 99 0 -120 -160 -120 -1 -1 15 -110 -11 42 2147483647 2147483647 gsm|lte
SignalStrength: 99 0 -120 -160 -120 -1 -1 15 -110 -11 42 2147483647 2147483647 gsm|lte
-------------------------------------------------------------------------

The event PSL_onSignalStrengthsChanged is fired only the first time then no more.
This appear only with nougat (v7).
With android 4.x, 5.x and 6.x no problem at all (the event is fired correctly)

Some hints?

Thanks,
Massimiliano
 

Massimiliano

Member
Licensed User
Longtime User
Yes, it's on foreground
This problem it's only on android 7 (7.1.1, I don't have other v7 versions to test)
With 4.03, 5.01, 5.1.1, 6.01 no problems at all: the event it's raised regularly
 
Last edited:
Upvote 0

Massimiliano

Member
Licensed User
Longtime User
Try to listen to the PhoneStateEvent with PhoneEvents, from a foreground service, and see whether it raises properly.
Yes, already tried PhoneStateEvent/PhoneEvents and it raises events regularly.

The problem seems to be related with all the events raised by PhoneStateListener events. :(
 
Upvote 0

Massimiliano

Member
Licensed User
Longtime User
This is what I'm seeing now with my example on an android 4.0.3 with psl.startListeningForEvent(psl.LISTEN_SIGNAL_STRENGTHS + psl.LISTEN_CELL_LOCATION)
As you can see, events are regularly raised (cell changes, signal strenghts)

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
PhoneStateListener has been initialized.
** Activity (main) Resume **
onCellLocationChanged [31120,26178604,4095]
[31120,26178604,4095]
onSignalStrengthChanged SignalStrength: 5 255 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767 gsm|lte
SignalStrength: 5 255 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767 gsm|lte
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
onCellLocationChanged [31120,26166275,4095]
[31120,26166275,4095]
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
onCellLocationChanged [-1,-1,-1]
[-1,-1,-1]
onSignalStrengthChanged SignalStrength: 99 255 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767 gsm|lte
SignalStrength: 99 255 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767 gsm|lte
onSignalStrengthChanged SignalStrength: 99 99 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767 gsm|lte
SignalStrength: 99 99 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767 gsm|lte
onCellLocationChanged [31120,26206384,4095]
[31120,26206384,4095]
onSignalStrengthChanged SignalStrength: 8 255 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767 gsm|lte
SignalStrength: 8 255 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767 gsm|lte
** Activity (main) Pause, UserClosed = false **
 
Upvote 0

Massimiliano

Member
Licensed User
Longtime User
Probably there is something about permissions although declared in manifest;
AddPermission("android.permission.ACCESS_COARSE_LOCATION")
AddPermission("android.permission.ACCESS_NETWORK_STATE")
AddPermission("android.permission.CHANGE_NETWORK_STATE")
AddPermission("android.permission.ACCESS_COARSE_UPDATES")
AddPermission("android.permission.READ_PHONE_STATE")

Setting the <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="25"/> (target 25)
now i have this error !!!!
------------------------------
PhoneStateListener has been initialized.
main_activity_create (java line: 336)
java.lang.SecurityException: Neither user 10109 nor current process has android.permission.ACCESS_COARSE_LOCATION.
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at com.android.internal.telephony.ITelephonyRegistry$Stub$Proxy.listenForSubscriber(ITelephonyRegistry.java:529)
at android.telephony.TelephonyManager.listen(TelephonyManager.java:3035)
at it.geniusoft.phonestatelistener.PSL.startListeningForEvent(PSL.java:72)
at it.geniusoft.phonestatelistener.PSL.startListening(PSL.java:59)
at b4a.example.main._activity_create(main.java:336)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
------------------------------

Reverting to <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21"/> (target 21)
The compiler give me this error while installing to device over target 25
->>> Failed to install PSL.apk: Failure [-26: Package b4a.example new target SDK 21 doesn't support runtime permissions but the old target SDK 25 does.]

Uninstalling and reinstalling with target 21, no more java.lang.SecurityException but PhoneStateListener doesn't fire events
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Massimiliano

Member
Licensed User
Longtime User
My guess is that developers used this event to cause their app to run all the time in the background, so Google made it more limited to prevent it.

Probably it's true Erel, I see much posts over stackoverflow.com with this problem

So in the meantime - with nougat - I'll try to switch detection behind a timer

Thank you all! ;)
 
Upvote 0

Massimiliano

Member
Licensed User
Longtime User
Now - with a workaround - it seems to work fine with android 7 and with help of the new B4A "sleep" function!

from post #1

B4X:
Sub PSL_onSignalStrengthsChanged(SignalStrength As String)
    Log(signalStrength)
    psl.stopListeningForEvent
    Sleep(3000) 'or whatever you want
    psl.startListening()
End Sub

Only Erel could say if it's OK :)

Massimiliano
 
Upvote 0
Top