#Region Project Attributes
#ApplicationLabel: TestOrient
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified 'IMPORTANT
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
'#BridgeLogger: True
Sub Process_Globals
Public ActionBarHomeClicked As Boolean
Public MyAppState As Map
Private accelerometer As PhoneSensors 'include Phone library
Private accValues() As Float
Private LastOrientation As Int
Private checkOrientation As Timer
Private rp As RuntimePermissions 'include RuntimePermissions library
Private myDir As String
Dim ser As B4XSerializator 'include RandomAccessFile library
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
MyAppState.Initialize
myDir = rp.GetSafeDirDefaultExternal("")
If FirstTime = False Then MyAppState = ser.ConvertBytesToObject(File.ReadBytes(myDir, "MyAppState.dat"))
Dim ph As Phone
Dim orientation As String = "Landscape"
If Activity.width < Activity.Height Then
ph.SetScreenOrientation(7)
LastOrientation = 7
orientation = "portrait"
Else
ph.SetScreenOrientation(6)
LastOrientation = 6
End If
Log("Activity started with orientation: " & orientation)
accelerometer.Initialize(accelerometer.TYPE_ACCELEROMETER)
checkOrientation.Initialize("chkOrient", 333)
checkOrientation.Enabled = True
Dim pm As B4XPagesManager
pm.Initialize(Activity)
End Sub
Sub chkOrient_Tick
accelerometer.StartListening("Accelerometer")
Sleep(300)
accelerometer.StopListening
End Sub
'The method for sensing orientation change is extracted from @emexes:
'https://www.b4x.com/android/forum/t...-with-portrait-orientation.106872/post-669094
Private Sub Accelerometer_SensorChanged (Values() As Float)
checkOrientation.Enabled = False
accValues = Values
Dim X As Float = accValues(0)
Dim Y As Float = accValues(1)
Dim ThisOrientation As Int = LastOrientation
If Abs(Y) > 7 Then ThisOrientation = 7
If Abs(X) > 7 Then ThisOrientation = 6
If ThisOrientation <> LastOrientation Then
File.WriteBytes(myDir, "MyAppState.dat", ser.ConvertObjectToBytes(MyAppState))
Activity.Finish
StartActivity(Me)
LastOrientation = ThisOrientation
Else
checkOrientation.Enabled = True
End If
End Sub
'Template version: B4A-1.01
#Region Delegates
Sub Activity_ActionBarHomeClick
ActionBarHomeClicked = True
B4XPages.Delegate.Activity_ActionBarHomeClick
ActionBarHomeClicked = False
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub
Sub Activity_Resume
B4XPages.Delegate.Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
B4XPages.Delegate.Activity_Pause
End Sub
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub
Sub Create_Menu (Menu As Object)
B4XPages.Delegate.Create_Menu(Menu)
End Sub
#if Java
public boolean _onCreateOptionsMenu(android.view.Menu menu) {
processBA.raiseEvent(null, "create_menu", menu);
return true;
}
#End If
#End Region
'Program code should go into B4XMainPage and other pages.