Italian PhoneStateListener non funziona

giada

Member
Ciao a tutti,
sto cercando di avere informazioni sul segnale gsm e le famose tacche, sono partita dall'esempio che ho trovato sul forum e inizialmente non funzionava ... ieri non so come sono riuscita a farlo partire e riuscivo a vedere la stringa "signalStrength" con tutte le info, oggi non va più..non capisco qual'è il problema di questa libreria...ho provato diversi esempi ma non riesco a capire...qualcuno ha dei suggerimenti?

Ecco il main

#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: PhoneStateListener
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#BridgeLogger : True
#End Region

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.


End Sub

Sub Activity_Create(FirstTime As Boolean)

Activity.AddMenuItem("Start","mnuStart")
Activity.AddMenuItem("Stop","mnuStop")


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub mnuStart_Click
StartService(Listener)
End Sub

Sub mnuStop_Click
StopService(Listener)
Listener.PSL.stopListening
End Sub

Ecco il service

#Region Module Attributes
#StartAtBoot: False
#End Region

'Service module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

'Declares the phone state listener
Dim PSL As PhoneStateListener

End Sub
Sub Service_Create

'Initializes the PhoneStateListener.
'All data is also outputted to the filtered log field.
PSL.Initialize("PSL",True)

'Starts listening
PSL.startListeningForEvent(PSL.LISTEN_DATA_ACTIVITY)

'Remember: Find all information about the returns here:
' http://developer.android.com/reference/android/telephony/PhoneStateListener.html

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub
' Callback invoked when the call-forwarding indicator changes.
Sub PSL_onCallForwardingIndicatorChanged(cfi As Boolean)

End Sub
'Callback invoked when device call state changes.
'Idle = 0
'Ringing = 1
'OFFHook = 2
Sub PSL_onCallStateChanged (State As Int, incomingNumber As String)

End Sub
' Callback invoked when device cell location changes.
Sub PSL_onCellLocationChanged(Location As String)

End Sub
'Callback invoked when data activity state changes.
Sub PSL_onDataActivity(Direction As Int, strDirection As String)
ToastMessageShow(strDirection,False)
End Sub
'Callback invoked when connection state changes.
Sub PSL_onDataConnectionStateChanged(state As Int, Connectionstate As String)

End Sub
'Callback invoked when the message-waiting indicator changes.
Sub PSL_onMessageWaitingIndicatorChanged(mwi As Boolean)

End Sub
'Callback invoked when device service state changes.
Sub PSL_onServiceStateChanged(State As Int, strState As String)

End Sub
'Callback invoked when network signal strengths changes.
'you can use getCDma, etc to get the network connection.
Sub PSL_onSignalStrengthsChanged (signalStrength As String)
ToastMessageShow(signalStrength, False)
ToastMessageShow(PSL.CdmaDbm, False)
Log(signalStrength)
End Sub

'Remember: Find all information about the returns here:
' http://developer.android.com/reference/android/telephony/PhoneStateListener.html
 

LucaMs

Expert
Licensed User
Longtime User
Ciao.

Quando pubblichi codice sorgente, immettilo nella dialog che compare premendo su:
1602588767279.png


Main:
B4X:
#Region Module Attributes
   #FullScreen: False
   #IncludeTitle: True
   #ApplicationLabel: PhoneStateListener
   #VersionCode: 1
   #VersionName: 
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
   #BridgeLogger : True
#End Region

'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.


End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   Activity.AddMenuItem("Start","mnuStart")
   Activity.AddMenuItem("Stop","mnuStop")
   

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub mnuStart_Click
   StartService(Listener)
End Sub

Sub mnuStop_Click
   StopService(Listener)
   Listener.PSL.stopListening
End Sub

Servizio Listener:
B4X:
#Region Module Attributes
   #StartAtBoot: False
#End Region

'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   'Declares the phone state listener
   Dim PSL As PhoneStateListener

End Sub
Sub Service_Create

   'Initializes the PhoneStateListener.
   'All data is also outputted to the filtered log field.
   PSL.Initialize("PSL",True)
   
   'Starts listening
   PSL.startListeningForEvent(PSL.LISTEN_DATA_ACTIVITY)
   
   'Remember: Find all information about the returns here:
   ' [URL]http://developer.android.com/reference/android/telephony/PhoneStateListener.html[/URL]

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub
' Callback invoked when the call-forwarding indicator changes.
Sub PSL_onCallForwardingIndicatorChanged(cfi As Boolean)
   
End Sub
'Callback invoked when device call state changes.
'Idle = 0
'Ringing = 1
'OFFHook = 2
Sub PSL_onCallStateChanged (State As Int, incomingNumber As String)

End Sub
' Callback invoked when device cell location changes.
Sub PSL_onCellLocationChanged(Location As String)

End Sub
'Callback invoked when data activity state changes.
Sub PSL_onDataActivity(Direction As Int, strDirection As String)
   ToastMessageShow(strDirection,False)
End Sub
'Callback invoked when connection state changes.
Sub PSL_onDataConnectionStateChanged(state As Int, Connectionstate As String)

End Sub
'Callback invoked when the message-waiting indicator changes.
Sub PSL_onMessageWaitingIndicatorChanged(mwi As Boolean)

End Sub
'Callback invoked when device service state changes.
Sub PSL_onServiceStateChanged(State As Int, strState As String)

End Sub
'Callback invoked when network signal strengths changes.
'you can use getCDma, etc to get the network connection.
Sub PSL_onSignalStrengthsChanged (signalStrength As String)
   ToastMessageShow(signalStrength, False)
   ToastMessageShow(PSL.CdmaDbm, False)
   Log(signalStrength)
End Sub

'Remember: Find all information about the returns here:
' [URL]http://developer.android.com/reference/android/telephony/PhoneStateListener.html[/URL]
 

LucaMs

Expert
Licensed User
Longtime User
A meno che tu non debba lasciare sempre attivo un servizio "in ascolto", anche ad app "chiusa", prova questa versione B4XPages.

[I progetti B4I e B4J puoi/dovresti toglierli, ovviamente]
 

Attachments

  • PhoneStateListenerTest.zip
    14.9 KB · Views: 196

giada

Member
grazie, funziona!!
solo che io lo devo integrare con servizi in ascolto ad app chiusa ... perchè quale problema c'è in questo caso?
 

giada

Member
ti chiedo un altra cosa: nella stringa che ottengo trovo le tacche del segnale, si capisce anche se è 4G H L altro?

SignalStrength:{mCdma=Invalid,mGsm=Invalid,mWcdma=Invalid,mTdscdma=Invalid,mLte=CellSignalStrengthLte: rssi=-79 rsrp=-105 rsrq=-10 rssnr=86 cqi=2147483647 ta=2147483647 level=3,mNr=Invalid,SignalBarInfo{ lteLevel=4 },T,rat=14,primary=CellSignalStrengthLte}
 

LucaMs

Expert
Licensed User
Longtime User

LucaMs

Expert
Licensed User
Longtime User
grazie, funziona!!
solo che io lo devo integrare con servizi in ascolto ad app chiusa ... perchè quale problema c'è in questo caso?
Nota che la versione B4XPages allegata continuerà a funzionare (mandare toast e log) anche se messa in standby, ovvero premendo Home o passando ad un'altra Activity.
Questo finché il sistema operativo non deciderà di "ucciderla" (se starà troppo tempo in standby e/o avrà bisogno di memoria libera).
 

Sagenut

Expert
Licensed User
Longtime User
Nella stringa di esempio che ci hai indicato a te interessa la parte evidenziata
primary=CellSignalStrengthLte
che è LTE.
Guardando nel link che ti inviato puoi vedere che LTE corrisponde al 4G
LTE.png

Devi quindi rilevare le altre sigle e vedere a cosa corrispondono.
Non sò dirti se quelle indicate nel link corrispondano esattamente a quelle fornite dalla app.
In pratica dalla stringa che ottieni devi scartare
primary=CellSignalStrength
La parte rimanente è quella da verificare per decidere cosa mostrare come risultato.
 

Sagenut

Expert
Licensed User
Longtime User
Se intendi che non sai come analizzare la stringa, ci sono diversi modi.
In ogni caso devi prima sapere quali risultati potrai ottenere per poi decidere cosa mostrare.
Un esempio potrebbe essere:
B4X:
Dim s As String = "primary=CellSignalStrengthLte" 'Tu otterrai la stringa nella variabile che vuoi
If s.ToUpperCase.Contains("LTE") Then Log("4G ATTIVO") 'Rendo la stringa tutta MAIUSCOLA per comodità e poi verifico che in questo caso contenga LTE
 

LucaMs

Expert
Licensed User
Longtime User
ciao @LucaMs

Come posso "intercettare" il numero chiamante e inoltrare la chiamata ad un altro numero?
Non credo sia possibile.

Puoi intercettare la chiamata e rilevare il numero nell'evento onCallStateChanged (in un modulo di servizio; vedi esempio nel post #2).
Puoi fargli effettuare una chiamata (al 99% previa conferma dell'utente, non automaticamente) usando:
B4X:
Dim mPhoneCalls As PhoneCalls
StartActivity(mPhoneCalls.Call("+39-1234..."))
ma non credo che tu possa chiudere la chiamata in arrivo:
I don't think that you will find any reliable way to dismiss the call.
 

Valentino.b

Member
Grazie @LucaMs and thank you @Erel

@LucaMs Dovendo trasferire la chiamata, non credo che la chiamata in ingresso debba essere chiusa. Inoltre quando arriva una chiamata, l'evento PSL_onCallStateChanged non viene scatenato.
@Erel In case of call forwarding, I suppose that the "original call" never dismiss.
 
Last edited:
Top