Android Question Autostart app

Declan

Well-Known Member
Licensed User
Longtime User
I was looking at this thread:
B4X:
https://www.b4x.com/android/forum/threads/auto-starting-your-app.37723/
Which has this code to autostart the app:
B4X:
#Region  Service Attributes
    #StartAtBoot: 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.
    Private tmrStart As Timer
End Sub
Sub Service_Create
    '--- set timer to start app after 1 seconds
    '--- this gives time for system to init other stuff
    tmrStart.Initialize("tmrStart",1000)
End Sub

Sub Service_Start (StartingIntent As Intent)
    '--- enabled timer
    tmrStart.Enabled = True
End Sub

Private Sub Service_Destroy
End Sub


Sub tmrStart_Tick
    tmrStart.Enabled = False
    '--- if we find the file 'autoStart.dat' in the folder 'File.DirInternal'
    '--- then auto start the app.
    Dim checkFile As String = "autoStart.dat"
    If File.Exists(File.DirInternal,checkFile) Then
        Log("Auto Start is enabled")
        StartActivity(Main)
    Else
        Log("Auto Start is disabled")
    End If
End Sub
Two "helper function" are provided to create the file that will auto-start or not auto-start your app:
B4X:
Sub SetAutoStartAppOn(turnON As Boolean)
    Dim checkFile As String = "autoStart.dat"
    If turnON Then
        If Not (File.Exists(File.DirInternal,checkFile)) Then
            File.WriteString(File.DirInternal,checkFile,"on")
        End If
    Else
        If File.Exists(File.DirInternal,checkFile) Then
            File.Delete(File.DirInternal,checkFile)
        End If
    End If
End Sub

Public Sub GetAutoStartAppOn() As Boolean
    Dim checkFile As String = "autoStart.dat"
    If File.Exists(File.DirInternal,checkFile) Then
        Return True
    Else
        Return False
    End If
End Sub
How are these two "helper function" files deployed?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Not sure I completely understand the question, but if you mean "where do I put the two helper functions", then you would put them in the Starter service along with the other code in the example.

- Colin.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
So, can I do the following:

Service "AutoBoot":
B4X:
#Region  Service Attributes
    #StartAtBoot: 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.
    Private tmrStart As Timer
End Sub
Sub Service_Create
    '--- set timer to start app after 1 seconds
    '--- this gives time for system to init other stuff
    tmrStart.Initialize("tmrStart",5000)
End Sub

Sub Service_Start (StartingIntent As Intent)
    '--- enabled timer
    tmrStart.Enabled = True
End Sub

Private Sub Service_Destroy
End Sub


Sub tmrStart_Tick
    tmrStart.Enabled = False
    '--- if we find the file 'autoStart.dat' in the folder 'File.DirInternal'
    '--- then auto start the app.

  
    Dim checkFile As String = "/eBuki/autoStart.dat"
    If File.Exists(File.DirRootExternal,checkFile) Then
        Log("Auto Start is enabled")
        StartActivity(Main)
    Else
        Log("Auto Start is disabled")
    End If
End Sub

Sub SetAutoStartAppOn(turnON As Boolean)
    Dim checkFile As String = "/eBuki/autoStart.dat"
    If turnON Then
        If Not (File.Exists(File.DirRootExternal,checkFile)) Then
            File.WriteString(File.DirRootExternal,checkFile,"on")
        End If
    Else
        If File.Exists(File.DirRootExternal,checkFile) Then
            File.Delete(File.DirRootExternal,checkFile)
        End If
    End If
End Sub

Public Sub GetAutoStartAppOn() As Boolean
    Dim checkFile As String = "/eBuki/autoStart.dat"
    If File.Exists(File.DirRootExternal,checkFile) Then
        Return True
    Else
        Return False
    End If
End Sub

Then in Service "Starter":
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals

' Animation Delay
    Public csu As CallSubUtils
    Dim PE As PhoneEvents
  
' SQL
    Dim SQL0 As SQL
End Sub

Sub Service_Create
  
' Switch Data Connection ON
    SwitchDataConnectionOn(True)
  

    PE.Initialize("PE")
  
' Delay Sub
    csu.Initialize
  
' Setup SQL
    SQL0.Initialize(File.DirRootExternal, "/eBuki/Data/ebuki.db", True)
End Sub


Sub Service_Start (StartingIntent As Intent)

End Sub

Sub PE_ConnectivityChanged (NetworkType As String, State As String, NetworkIntent As Intent)
    CallSub2(Menu, "ConnectionChange", State)
    CallSub2(Menu, "NetworkConn", NetworkType)
End Sub

Sub PE_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, BatteryIntent As Intent)
    CallSub2(Menu, "BatteryChange", Level)
    CallSub2(Menu, "BatteryPlugged", Plugged)
End Sub

Sub SwitchDataConnectionOn(dataState As Boolean)
    Dim ph As Phone
    Dim Command, Runner As String
    Dim StdOut, StdErr As StringBuilder
    Dim Result As Int
 
    StdOut.Initialize
    StdErr.Initialize
    Runner = File.Combine(File.DirInternalCache, "runner")
    Command = File.Combine(File.DirInternalCache, "command")
    File.WriteString(File.DirInternalCache, "runner", "su < " & Command)

    If dataState Then         ' turn data connection on
        File.WriteString(File.DirInternalCache, "command", "svc data enable")
'    Else            ' turn data connection off
'        File.WriteString(File.DirInternalCache, "command", "svc data disable")
    End If
    Result = ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

The Service "AutoBoot" is set: #StartAtBoot: True
The Service "Starter" is set: #StartAtBoot: False

This does not work?
When I run in Debug, I see:
B4X:
** Service (newinst2) Start **
---- AppUpdating.newinst2: service_started
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **

So, I assume that the "Starter" Service is started, but not "AutoBoot" Service?
 
Upvote 0
Top