B4A Library PhoneStateListener

Hey everyone,

After a request made by bluejay at this page , I developed the PhoneStateListener Library.
With the PhoneStateListener, you are able to retrieve several kinds of information when something happens on your phone, like someone is calling, when you mobile phone signal strength changes, celllocation changes, etc.

PhoneStateListener
Author:
XverhelstX
Version: 1.3
  • PhoneStateListener
    Fields:
    • LISTEN_CALL_FORWARDING_INDICATOR As Int
    • LISTEN_CALL_STATE As Int
    • LISTEN_CELL_LOCATION As Int
    • LISTEN_DATA_ACTIVITY As Int
    • LISTEN_DATA_CONNECTION_STATE As Int
    • LISTEN_MESSAGE_WAITING_INDICATOR As Int
    • LISTEN_NONE As Int
    • LISTEN_SERVICE_STATE As Int
    • LISTEN_SIGNAL_STRENGTHS As Int
    Methods:
    • Initialize (EventName As String, EnableLogging As Boolean)
      Initializes the PhoneStateListener.
      EventName - Events subs prefix.
    • isGSM As Boolean
    • objCellLocation As Object
      Returns the CellLocation.
      CellLocation has a value when onCellLocationChanged is called.
      You can work with this object in the Reflector library.
      Return type: @return:
    • objServiceState As Object
      Returns the ServiceState.
      ServiceState1 has a value when onServiceStateChanged is called.
      You can work with this object in the Reflector library.
      Return type: @return:
    • objSignalStrength As Object
      Returns the SignalStrength.
      SignalStrength has a value when onSignalStrengthsChanged is called.
      You can work with this object in the Reflector library.
      Return type: @return:
    • startListening
      Starts listening for all PhoneStateListener Events.
      Use startListeningForEvent for specific events.
    • startListeningForEvent (Event As Int)
      Starts listening for specific event in the phone.
      See the LISTEN_Attributes
    • stopListening
      Stops listening for all PhoneStateListener Events.
      Use stopListeningForEvent to stop specific events.
    • stopListeningForEvent
      Stops listening for PhoneStateListener Events called from StartListeningForEvent.
    Permissions:
    • android.permission.ACCESS_COARSE_LOCATION
    • android.permission.ACCESS_COARSE_UPDATES
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.CHANGE_NETWORK_STATE
    • android.permission.READ_PHONE_STATE
    Properties:
    • CdmaDbm As Int [read only]
      Get the CDMA RSSI value in dBm
    • CdmaEcio As Int [read only]
      Get the CDMA Ec/Io value in dB*10
    • CellLocation As String [read only]
      Gets the cell location in a string.
    • EvdoDbm As Int [read only]
      Get the EVDO RSSI value in dBm
    • EvdoEcio As Int [read only]
      Get the EVDO Ec/Io value in dB*10
    • EvdoSnr As Int [read only]
      Get the signal to noise ratio.
    • GsmBitErrorRate As Int [read only]
      Get the GSM bit error rate (0-7, 99) as defined in TS 27.007 8.5
    • GsmSignalStrength As Int [read only]
      Get the GSM Signal Strength, valid values are (0-31, 99) as defined in TS 27.007 8.5


* When onSignalStrengthsChanged is called, you can automatically use all the get methods (like getGsmSignalStrength, getEvdoEcio(), ...).

* A lot of details is outputted to the filtered B4A logs, so don't forget to check that out too.

The best thing, is that it is very easy to use and implement in your app.
Just add a service and copy the following:

B4X:
'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")
  
   'Starts listening
   PSL.startListening()
   ToastMessageShow(PSL.CellLocation,False)
  
   '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)

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)
End Sub

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

Don't forget to start the service with StartService and stop with Stopservice.

The attachment contains a sample and the library files.

More information can be found here:
PhoneStateListener | Android Developers

Feedback, criticism and found bugs are welcome and will be fixed asap.

Tomas
 

Attachments

  • PhoneStateListener1.1.zip
    288.9 KB · Views: 1,079
  • PhoneStateListener1.2.zip
    292.6 KB · Views: 1,006
  • PhoneStateListener1.3.zip
    292.6 KB · Views: 2,390
Last edited:

bluejay

Active Member
Licensed User
Longtime User
I noticed the same issue with LISTEN_SIGNAL_STRENGTH and switched to the new LISTEN_SIGNAL_STRENGTHS. This seemed to work well for GSM but I am not sure about 3G.

Note that LISTEN_SIGNAL_STRENGTHS requires Android 2.1 and higher.

About the library code:

In ver 1.2 you used the following constructor
private final PhoneStateListener phoneStateListener = new PhoneStateListener()

But in ver 1.3 this became:
private final PhoneStateListener phoneStateListener = new PSL.1(this);

private final PhoneStateListener phoneStateListenerForEvent = new PSL.2(this);

The two 5 kbyte class files PSL$1.class and PSL$2.class seem to contain no classes so the one in v1.2 looks more correct to me.:confused:

My apologies if I seem to be getting pedantic, eventually I hope figure out how this library creation stuff works.

bluejay
 

zabec

New Member
Licensed User
Longtime User
Hi!
I'm new to all of this. It seem very interesting. From your link to android.telephony, if I understand right, you've implemented PhoneStateListener class (which is obvious from name). Is it a lot of work to do the same for ServiceState class?
I need application to easily/quickly change operator. I would have 2 operators from which I would choose and have them selected manually (no automatic operator selection).
Can you tell me, if this would be possible with ServiceState class?

Thank you.
 

flyingbag

Member
Licensed User
Longtime User
Hello,
The PSL.CellLocation gives me something like [504,33073604,-1]
What does this mean?
Thank you...
Flyingbag
 

wwzw

New Member
Licensed User
Longtime User
Hello,
The PSL.CellLocation gives me something like [504,33073604,-1]
What does this mean?
Thank you...
Flyingbag

I also like this!
The PSL.CellLocation Or PSL_onCellLocationChanged(Location As String) result like [35554,463586,1734846,14179,12]

I hope to longitude and latitude information!

Who can help me ! Thanks !
 

konisek

Member
Licensed User
Longtime User
Can I use this library when I want to read the signal strength just once? I mean:


B4X:
Dim PSL As PhoneStateListener
...
Sub button1_click
    Msgbox(PSL.CellLocation,"")
    Msgbox(PSL.GsmSignalStrength,"")
End Sub


The first Msg results in
[3812,242559177,-1]
but
the second Msg in:

An error has occured in
sub:mainbutton1_click
(java line:234)
java.lang.
NullPointerException
Continue?
 

sasidhar

Active Member
Licensed User
Longtime User
Hey everyone,

After a request made by bluejay at this page , I developed the PhoneStateListener Library.
With the PhoneStateListener, you are able to retrieve several kinds of information when something happens on your phone, like someone is calling, when you mobile phone signal strength changes, celllocation changes, etc.

PhoneStateListener
Author:
XverhelstX
Version: 1.3
  • PhoneStateListener
    Fields:
    • LISTEN_CALL_FORWARDING_INDICATOR As Int
    • LISTEN_CALL_STATE As Int
    • LISTEN_CELL_LOCATION As Int
    • LISTEN_DATA_ACTIVITY As Int
    • LISTEN_DATA_CONNECTION_STATE As Int
    • LISTEN_MESSAGE_WAITING_INDICATOR As Int
    • LISTEN_NONE As Int
    • LISTEN_SERVICE_STATE As Int
    • LISTEN_SIGNAL_STRENGTHS As Int
    Methods:
    • Initialize (EventName As String, EnableLogging As Boolean)
      Initializes the PhoneStateListener.
      EventName - Events subs prefix.
    • isGSM As Boolean
    • objCellLocation As Object
      Returns the CellLocation.
      CellLocation has a value when onCellLocationChanged is called.
      You can work with this object in the Reflector library.
      Return type: @return:
    • objServiceState As Object
      Returns the ServiceState.
      ServiceState1 has a value when onServiceStateChanged is called.
      You can work with this object in the Reflector library.
      Return type: @return:
    • objSignalStrength As Object
      Returns the SignalStrength.
      SignalStrength has a value when onSignalStrengthsChanged is called.
      You can work with this object in the Reflector library.
      Return type: @return:
    • startListening
      Starts listening for all PhoneStateListener Events.
      Use startListeningForEvent for specific events.
    • startListeningForEvent (Event As Int)
      Starts listening for specific event in the phone.
      See the LISTEN_Attributes
    • stopListening
      Stops listening for all PhoneStateListener Events.
      Use stopListeningForEvent to stop specific events.
    • stopListeningForEvent
      Stops listening for PhoneStateListener Events called from StartListeningForEvent.
    Permissions:
    • android.permission.ACCESS_COARSE_LOCATION
    • android.permission.ACCESS_COARSE_UPDATES
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.CHANGE_NETWORK_STATE
    • android.permission.READ_PHONE_STATE
    Properties:
    • CdmaDbm As Int [read only]
      Get the CDMA RSSI value in dBm
    • CdmaEcio As Int [read only]
      Get the CDMA Ec/Io value in dB*10
    • CellLocation As String [read only]
      Gets the cell location in a string.
    • EvdoDbm As Int [read only]
      Get the EVDO RSSI value in dBm
    • EvdoEcio As Int [read only]
      Get the EVDO Ec/Io value in dB*10
    • EvdoSnr As Int [read only]
      Get the signal to noise ratio.
    • GsmBitErrorRate As Int [read only]
      Get the GSM bit error rate (0-7, 99) as defined in TS 27.007 8.5
    • GsmSignalStrength As Int [read only]
      Get the GSM Signal Strength, valid values are (0-31, 99) as defined in TS 27.007 8.5


* When onSignalStrengthsChanged is called, you can automatically use all the get methods (like getGsmSignalStrength, getEvdoEcio(), ...).

* A lot of details is outputted to the filtered B4A logs, so don't forget to check that out too.

The best thing, is that it is very easy to use and implement in your app.
Just add a service and copy the following:

B4X:
'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")
 
   'Starts listening
   PSL.startListening()
   ToastMessageShow(PSL.CellLocation,False)
 
   '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)

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)
End Sub

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

Don't forget to start the service with StartService and stop with Stopservice.

The attachment contains a sample and the library files.

More information can be found here:
PhoneStateListener | Android Developers

Feedback, criticism and found bugs are welcome and will be fixed asap.

Tomas



Hi,

In Listener service if I make StartAtBoot = True then the application if not working, I have created a new service and made it StartBoot = True,
and called the Listener service still the application is not working. Without manual intervention(start/stop service) the application should work.Any chance pls..

Thanks
Sasidhar.M
 

zs5cey

Member
Licensed User
Longtime User
I am running this library on a dual sim device. It only shows the information for the 2nd SIM. How do I tell the listener which SIM information I want?
 

Rusty

Well-Known Member
Licensed User
Longtime User
Does anyone know or have a table for the GSM signal strengths.
(i.e. indeterminate 99
Poor 1-5
Good 6-20
...
The GSM signal strengths I'm getting on a non LTE tablet (cellular) are ranges of positive integers (i.e. 10,7, 4, 5...etc.)
Thanks in advance.
Rusty
 
Top