Android Question [Solved using workaround] How to extract info from a bundle

mark35at

Well-Known Member
Licensed User
Sorry if this is so simple but I have no idea what a bundle is. I am trying to get info from the battery using the code below.

B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private pe As PhoneEvents
End Sub
Sub Service_Create
    pe.Initialize("pe")
End Sub

Sub pe_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    Main.BatteryCharging=Plugged
    Main.BatteryLevel=Level
    Main.BatteryScale=Scale
   
    'Log(Intent.ExtrasToString)
   
    Dim BatteryProperties(), tString As String
    Dim tValue As Double
    BatteryProperties=Regex.Split(",",Intent.ExtrasToString)
   
    Main.BatteryVoltageString=BatteryProperties(12)
    Main.BatteryTemperatureString=BatteryProperties(6)
   
    Main.BatteryCharging=Plugged                                    'delete when finished
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub
This is a service which runs and triggers the changed event immediaely. I see the info in the intent and can split the values badly into BatteryProperties. I would like just the values and not the text. Thanks for any help.
 

DonManfred

Expert
Licensed User
see a wrokaround here
B4X:
Sub pe_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    Log("BatteryChanged("&Level&","&Scale&","&Plugged)
    'Main.BatteryCharging=Plugged
  'Main.BatteryLevel=Level
  'Main.BatteryScale=Scale
  
  Log(Intent.ExtrasToString)
  
  Dim BatteryProperties(), tString As String
  Dim props As Map
    props.Initialize
    Dim tValue As Double
  Dim dummy As String = Intent.ExtrasToString
    Intent.
    dummy.Replace("Bundle[{","")
    dummy.Replace("}","")
    If dummy.Length > 0 Then
        dummy = dummy.Replace("Bundle[{","")
        dummy = dummy.Replace("}]","").Trim
        dummy = dummy.Trim
    End If
    Log(dummy)
    BatteryProperties=Regex.Split(",",dummy)
  For i = 0 To BatteryProperties.Length-1
        Dim values() As String = Regex.Split("=",BatteryProperties(i))
        props.Put(values(0).Trim,values(1).Trim)
        'Log(BatteryProperties(i))
    Next
    Log(props)
    'Bundle[{technology=Li-ion, icon-small=17303200, health=2, status=5, plugged=2, present=true, level=100, scale=100, temperature=252, voltage=4337, invalid_charger=0}]
  'Main.BatteryVoltageString=BatteryProperties(12)
  'Main.BatteryTemperatureString=BatteryProperties(6)
  
  'Main.BatteryCharging=Plugged                                    'delete when finished
End Sub
but i believe it is the wrong way. You need to get the extras by its name i suppose to get the right ones...

B4X:
    If Intent.HasExtra("technology") Then
        Log("Technology: "&Intent.GetExtra("technology"))
    End If
 

mark35at

Well-Known Member
Licensed User
Thx Manfred. You have put me in the right direction. I have all the properties in a map or at least I can see them with rapid debugger, going into BatteryProperties BUT The sub SysList6_Fill fills the values with Null.
I think the service is called too late but I am not sure. I have uploaded the whole project. Maybe you can find my mistake.
 

Attachments

mark35at

Well-Known Member
Licensed User
No sooner had I uploaded an answer, I found the problem. The SysList_Fill WAS being called before the service could start and trigger the event.
I made a timer for 5 seconds and in the tick event, placed the SysList_Fill. Now everything works.

Still don't know what a bundle is! The workaround is okay. Here is the complete project working to date.
 

Attachments

Top