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:

agraham

Expert
Licensed User
Longtime User
You are still unnecessarily including the Java source code in the jar!

You could save the serviceState parameter of onServiceStateChanged and make it available as an Object returned from a method called, for example, getServiceState. It would then be available to play with using a Reflector if it were necessary or interesting to do so.
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
You are still unnecessarily including the Java source code in the jar!

Yes, sorry, this was my fault.
I played with another library, trying to port it, and it had to include the resource files. (atleast I thought it had).
Then i forgot to uncheck the box again.

You could save the serviceState parameter of onServiceStateChanged and make it available as an Object returned from a method called, for example, getServiceState. It would then be available to play with using a Reflector if it were necessary or interesting to do so.

I always wondered how I could implement something that is available in Eclipse but not in Basic4Android in a library. Just like serviceState and SignalStrength.

I will update the library with the above 2 mentioned.

Tomas
 

bluejay

Active Member
Licensed User
Longtime User
Hi Tomas,

Looks good, nice work. I just got home, can't wait to try it out.

Some feedback:

a) Can we add a flag parameter to PSL.Initialise to indicate which events we want to listen for?

b) Can this flag include NO_LISTEN so we can turn the listening on an off without having a separate service in an app that already has a service running.

c) As the Android docs indicate some of the events are 'very' frequent. The logging is good for library testing but should be minimised for the final version.

One reason I wanted this was to have a 'battery friendly' way getting notified of changes the phone state.

I will let you know how my testing goes.

Thanks again, much appreciated.

regards,

bluejay
 

bluejay

Active Member
Licensed User
Longtime User
Correction.

The flag should be a parameter of PSL.startListening.

Maybe 'enableLogging' could be a parameter of of PSL.Initialize.

The sample project seems to be for cdma phones, need to use test for GSM.

bluejay
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
Updated to 1.2.

Now contains:
PSL.Initialize(EventName as String, EnableLogging as Boolean)

PSL.Startlistening

PSL.StartlisteningForEvent(int Event)
e.g
PSL.StartlisteningForEvent(PSL.LISTEN_CELL_LOCATION)

PSL.Stoplistening()

PSL.StopListeningForEvent()


I hope this is what u are after.

Tomas
 

bluejay

Active Member
Licensed User
Longtime User
:wav:

Terrific! Looks perfect. A great addition to the B4A libraries.

Question:
In the jar file there is also a PSL$1.class and PSL$2.class which don't seem to be needed - can I just delete these from the jar file?

Thanks for making this library.:)
 

Ronny

Member
Licensed User
Longtime User
onSignalStrengthsChanged never triggers.

Thanks for the library :)

I have made a little test-app for myself and I find that the onSignalStrengthsChanged event never triggers. Where I live I have a fluxating signalstrength from zero to full, but it never registers. Any ideas?
 

Ronny

Member
Licensed User
Longtime User
Log is on. Every other event works just fine. I use startListening. I'll check with StartListeningForEvent and post a reply here after the test.
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
I think I found the problem.
Please redownload 1.3 and check if it works.

@BlueJay,; I'm not sure if you can. I think I only added 1 class.
You can experiment and see if it works ;) otherwise, redownload and use 1.3

Thanks

Tomas
 
Last edited:
Top