Android Question Reading Title Bar Signal Strength.

potman100

Active Member
Licensed User
Longtime User
Hi

Has anybody got a working solution to this, I have searched far and wide and can't seem to get it working.

The general advice is to use the PhoneStateListener in a service and capture the PSL_onSignalStrengthsChanged event, I have tried this but the event never fires.

You used to be able to call GsmSignalStrength from PhoneStateListener and this would give signal data but it now returns a null reference error.

It must be possible ??

Thanks

Potman100
 

potman100

Active Member
Licensed User
Longtime User
Hi Erel

No, it does not get any data from onSignalStrengthsChanged when set tot sdk 19, it does however fire onDataActivity occasionally.

Regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
create a new project and put this code in the Starter service:
B4X:
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 Service_Create
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim TelephonyManager As JavaObject = ctxt.RunMethod("getSystemService", Array("phone"))
   Dim listener As JavaObject
   listener.InitializeNewInstance(Application.PackageName & ".starter.MyPhoneStateListener", Null)
   TelephonyManager.RunMethod("listen", Array(listener, 0x00000100))
End Sub

Sub Service_Start (StartingIntent As Intent)
   

End Sub

Sub Service_TaskRemoved
   'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

Sub Service_Destroy

End Sub

#if Java

import android.telephony.*;
public static class MyPhoneStateListener extends PhoneStateListener {
public MyPhoneStateListener() {
}
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        anywheresoftware.b4a.BA.Log("onSignalStrengthsChanged");
    }
}
#End If

Do you see the log message?

Based on the documentation it doesn't require any permission. If it doesn't work then add to the manifest editor:
B4X:
AddPermission(android.permission.READ_PHONE_STATE)
And set the targetSdkVersion to 19 (to avoid runtime permissions issues). Its just for testing.
 
Upvote 0

potman100

Active Member
Licensed User
Longtime User
Hi

Ye that works fine, changed the version to 19, all ok, also ran at version 26 and all ok.

So, how would I implement this to get the actual signal ? Sorry don't really understand how the Java stuff.

Thanks Erel.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code with event:
B4X:
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 Service_Create
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim TelephonyManager As JavaObject = ctxt.RunMethod("getSystemService", Array("phone"))
   Dim listener As JavaObject
   listener.InitializeNewInstance(Application.PackageName & ".starter.MyPhoneStateListener", Null)
   TelephonyManager.RunMethod("listen", Array(listener, 0x00000100))
End Sub

Sub Signal_Changed (SignalStrengh As Object)
   Dim s As JavaObject = SignalStrengh
   Log(s.RunMethod("getGsmSignalStrength", Null))
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   

End Sub

Sub Service_TaskRemoved
   'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

Sub Service_Destroy

End Sub

#if Java

import android.telephony.*;
public static class MyPhoneStateListener extends PhoneStateListener {
public MyPhoneStateListener() {
}
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        anywheresoftware.b4a.BA.Log("onSignalStrengthsChanged");
       starter.processBA.raiseEventFromUI(null, "signal_changed", signalStrength);
    }
}
#End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…