#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: 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 accelerometer As PhoneSensors
Private accValues() As Float
Dim LastOrientation As Int
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
If FirstTime Then
accelerometer.Initialize(accelerometer.TYPE_ACCELEROMETER)
End If
End Sub
Sub Activity_Resume
accelerometer.StartListening("Accelerometer")
End Sub
Sub Activity_Pause (UserClosed As Boolean)
accelerometer.StopListening
End Sub
Private Sub Accelerometer_SensorChanged (Values() As Float)
accValues = Values
Dim X As Float = accValues(0)
Dim Y As Float = accValues(1)
Dim Z As Float = accValues(2)
If False Then
Dim G As Float = Sqrt(X * X + Y * Y + Z * Z)
Dim S As String = "XYZ ="
S = S & " " & NumberFormat2(accValues(0), 1, 3, 3, False)
S = S & " " & NumberFormat2(accValues(1), 1, 3, 3, False)
S = S & " " & NumberFormat2(accValues(2), 1, 3, 3, False)
Log(S)
End If
Dim ThisOrientation As Int = LastOrientation
If Y > 7 Then
ThisOrientation = 8
else if X > 7 Then
ThisOrientation = 4
else if X < -7 Then
ThisOrientation = 6
End If
If ThisOrientation <> LastOrientation Then
Select Case ThisOrientation
Case 4: LogColor("Orientation Landscape Left", Colors.Blue)
Case 6: LogColor("Orientation Landscape Right", Colors.Blue)
Case 8: LogColor("Orientation Portrait", Colors.Blue)
End Select
LastOrientation = ThisOrientation
End If
End Sub