Android Question [Solved] Save ASCII file stopped by Standby mode?

Mark Read

Well-Known Member
Licensed User
Longtime User
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?

Many thanks for any help.
 

inakigarm

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
To be quite honest, I did not think about this problem at all, so the answer is no. I have never used services at all so this is new for me.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
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.

My B4A is 5.02, have I missed something??
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
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.

Although this works, will I still get problems?
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
The starter service was added in a newer version of B4A.

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.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I can add the starter service myself
But you need the correcponding b4a version to use it. Update your B4A to be able to use the starter Service.
as my version is too old?
correct. In your B4A the service you just added is "just a service like every other" with the name Starter.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I have read the tutorials :(. If I have understood correctly, I only need to add to Main, Activity create

B4X:
StartService(Starter)

with the code in post#15 and it should work. Correct?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
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. :D. My first service, even if is a simple one.
 
Upvote 0
Top