phone sensors in a service?

fanfalveto

Active Member
Licensed User
Longtime User
the log in add_sensors is ok but if i move the phone don´t do anything
B4X:
#Region  Service Attributes 
   #StartAtBoot: False
#End Region
Sub Process_Globals
Dim mp As MediaPlayer
Dim p As PhoneWakeState
Dim SensorsMapa As Map
Dim ps As PhoneSensors   
Dim poss  As Float
   Dim poss2 As Float
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
SensorsMapa.Initialize
       'This object is only used to access the type constants.
      AddSensor(ps.TYPE_ORIENTATION, "ORIENTACION: ", True)
      For i = 0 To SensorsMapa.Size - 1
      ps = SensorsMapa.GetKeyAt(i)   
   Next
End Sub
Sub Service_Destroy
ExitApplication
End Sub
Sub sonido
If poss2=0 Then
Else If poss-poss2>10 OR poss-poss2<-10 Then
      mp.Load(File.DirAssets,"grito.mp3")
      mp.play      
poss2=poss
      End If
End Sub
Sub AddSensor(SensorType As Int, Name As String, ThreeValues As Boolean) As SensorData
   Dim sd As SensorData
   Dim ps As PhoneSensors
   sd.Initialize
   sd.Name = Name
   sd.ThreeValues = ThreeValues   
   ps.Initialize(SensorType)
   SensorsMapa.Put(ps, sd)
   Log(Name & " MaxValue111111 = " & ps.MaxValue)
End Sub
Sub Sensor_SensorChanged (Values() As Float)
Dim pss As PhoneSensors
Dim sd As SensorData
   ps = Sender
   sd = SensorsMapa.Get(ps) 'Get the associated SensorData obejct
   poss=NumberFormat(Values(0), 0, 3)
   sonido
   Return poss
End Sub
 

derez

Expert
Licensed User
Longtime User
B4X:
 Dim ps As PhoneSensors
My guess:
This declaration appears in the global, so repeating it in add_sensor is changing it to a local object, hence it cannot call the event.
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
Yes,service is started,iput a log tío see start,i rey servicestart foreground.thanks

Enviado desde mi HTC Desire usando Tapatalk 2
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
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.
Dim mp As MediaPlayer
Dim p As PhoneWakeState
Dim SensorsMapas As Map
Dim pss As PhoneSensors
   Type SensorDatas (Name As String, ThreeValues As Boolean)
   Dim n As Notification
Dim poss  As Float
   Dim poss2 As Float
   Dim sdd As SensorDatas
End Sub
Sub Service_Create
SensorsMapas.Initialize
n.Initialize
n.Sound=False
Service.StartForeground(1,n)
End Sub

Sub Service_Start (StartingIntent As Intent)
p.ReleaseKeepAlive
p.PartialLock
p.ReleasePartialLock
       'This object is only used to access the type constants.
      AddSensor(pss.TYPE_ORIENTATION, "ORIENTACION: ", True)
      For i = 0 To SensorsMapas.Size - 1
      pss = SensorsMapas.GetKeyAt(i)   
   Next
End Sub

Sub Service_Destroy
ExitApplication
End Sub
Sub sonido
If poss2=0 Then

Else If poss-poss2>10 OR poss-poss2<-10 Then
      mp.Load(File.DirAssets,"grito.mp3")
      mp.play      
poss2=poss
Log("mov: "&poss2)
      End If
End Sub
Sub AddSensor(SensorType As Int, Name As String, ThreeValues As Boolean) As SensorData   
   sdd.Initialize
   sdd.Name = Name
   sdd.ThreeValues = ThreeValues   
   pss.Initialize(SensorType)
   SensorsMapas.Put(pss, sdd)
   Log(Name & " MaxValue111111 = " & pss.MaxValue)
End Sub

Sub Sensor_SensorChanged (Values() As Float)
   'Get the PhoneSensors object that raised this event.
   pss = Sender
   sdd = SensorsMapas.Get(pss) 'Get the associated SensorData obejct
   poss=NumberFormat(Values(1), 0, 3)
Log("cambia")
   sonido
   Return poss
End Sub
i see
B4X:
Log(Name & " MaxValue111111 = " & pss.MaxValue)
but
B4X:
Log("cambia")
not if i move the phone.
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
i do what i want with
B4X:
Dim acel As PhoneAccelerometer
B4X:
Sub Accelerometer_AccelerometerChanged (X As Float, Y As Float,Z As Float)
If Y>.5 AND suena =1 Then
Log ("Y: "&Y)
Log("X: "&X)
poss=Y
suena =0
    sonido
   Return poss   
   End If
End Sub
from an Erel example,thank you
 
Upvote 0
Top