Android Example Auto starting your app

Hi all!

Here is what you need to auto start your app at boot up.
Create a service module and paste in the code below:


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

This service module will now auto start at boot up. if it finds a file 'autoStart.dat' in the 'File.DirInternal' folder it will activate the main activity and auto start your app.

Here is a helper function 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

Have fun!!!
 
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
Jake, this is pretty cool... Out of curiosity (I actually want to do this), is there a way to start your app up at a given time rather than at boot? If someone starts up the app, but doesn't turn their phone off, I'd want the app to automatically run at say &:45am or 6:15pm...
 

barx

Well-Known Member
Licensed User
Longtime User
Jake, this is pretty cool... Out of curiosity (I actually want to do this), is there a way to start your app up at a given time rather than at boot? If someone starts up the app, but doesn't turn their phone off, I'd want the app to automatically run at say &:45am or 6:15pm...
look up StartServiceAt
 

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks!
 

sorex

Expert
Licensed User
Longtime User
If I have 3 services (network, server, client) do I need to set those all 3 to StartAtBoot or just 1 that opens the activity where the other 2 are started?
 

trueboss323

Active Member
Licensed User
Longtime User
It is good, but only 1 problem, is there a way I can start the app minimized? That way it is already loaded in memory?
 

trueboss323

Active Member
Licensed User
Longtime User
Here is what I have:
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
 

raphael75

Active Member
Licensed User
Longtime User
What is the timer good for? Some sort of delay?
The reason is written in the comments :)
B4X:
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
 
Top