I have a B4A app which runs for nearly 24 hours. This is my first test. It is running on a tablet and aquires data via bluetooth which is then written to an ASCII file each second. This is obviously a lot of data but is required.
The problem is although the timer keeps running when the screen shutsdown, the data is no longer written to the file. If I reactivate the tablet by touching the data is written again.
How can I overcome this problem? Do I need to use a lock to keep the screen active?
Have you set a Partial Lock (CPU running, screen off) ??
On starter Service:
B4X:
Sub Process_Globals
Dim P1 As PhoneWakeState
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
P1.PartialLock
End Sub
Just to expand: It is not a problem if the screen goes off but the file must be written. The tablet will be unattented during the measurement overnight.
I just checked the file. The timer does not keep running. The way I calculate the elapsed time is to store the start time and subtract it from the time now. At first I thought the timer was still running but it is not.
So I have added a service called Starter containing
B4X:
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#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 P1 As PhoneWakeState
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
P1.PartialLock
Log("Aquiring partial lock")
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Destroy
End Sub
Wenn I run the app, I do not see the "Aquiring partial lock" using USB mode.
The permission was added to the manifest automatically.
Took out the service and added to the main activity:
B4X:
Sub Process_Globals
Dim P1 As PhoneWakeState
End Sub
B4X:
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
P1.KeepAlive(False)
....
B4X:
Sub btn_Quit_Click
P1.ReleaseKeepAlive
Activity.Finish
End Sub
....
The screen off is set (in settings) for 1 minute. Now the screen goes dim but the app continues. Just what I needed. Thanks to all.
According to the user guide, I can add the starter service myself. Does this mean that it will not work as my version is too old? I tried to add the service but it doesn't seem to work. At least the screen still turns off completely and the cpu stops.
Is there another option? This was my planned service.
B4X:
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#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 P1 As PhoneWakeState
Dim n As Notification
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
n.Initialize
n.Icon="icon"
P1.KeepAlive(False)
End Sub
Sub Service_Start (StartingIntent As Intent)
Log("Aquiring partial lock")
Service.StartForeground( 1,n)
End Sub
Sub Service_Destroy
P1.ReleaseKeepAlive
End Sub
Can I maybe call this from Main, Activity create? Sorry I am not used to using services.
So, had a few errors with the notification in post#15.
Changed the starter service to:
B4X:
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#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 P1 As PhoneWakeState
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
P1.KeepAlive(False)
End Sub
Sub Service_Start (StartingIntent As Intent)
Log("Aquiring partial lock")
Service.StartForeground(0,Null)
End Sub
Sub Service_Destroy
P1.ReleaseKeepAlive
End Sub
Added in Main:
B4X:
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
StartService(Starter)
and in the quit sub:
B4X:
Sub btn_Quit_Click
Dim result As Int
result=Msgbox2( "Do you really want to exit?","Confirmation required.","Yes","","No",Null)
If result=DialogResponse.NEGATIVE Then
Return
End If
If astream.IsInitialized Then astream.Close
If serial1.IsInitialized Then serial1.Disconnect
StopService(Starter)
Activity.Finish
End Sub
The screen is set to turnoff in 1 minute. After 54 seconds it goes dim but the app runs.
Many thanks for the help. . My first service, even if is a simple one.