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:

arnold steger

Member
Licensed User
Longtime User
my service not open the app. what is my error?
I want read evrytime the sms, when contain "My Text send from app" then whant start my app and added a marker
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.
    Type Message (Address As String, Body As String)
    Dim TextSMSvorhanden As Boolean
   
End Sub
Sub Service_Create
    TextSMSvorhanden=False
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
      Dim messages() As Message
      messages = ParseSmsIntent(StartingIntent)
      For i = 0 To messages.Length - 1
         Log(messages(i))
      Next
   End If
   
If Main.SucheTextSMS.Contains("(My Text send from app)") Then
        TextSMSvorhanden=True
    StartActivity(Main)
    If Main.gmapinit=True AND TextSMSvorhanden Then
    CallSubDelayed(Main,"MarkerSetzenAusSMS")
    End If
End If
           
End Sub

Sub Service_Destroy

End Sub

'Parses an SMS intent and returns an array of messages
Sub ParseSmsIntent (In As Intent) As Message()
   Dim messages() As Message
   If In.HasExtra("pdus") = False Then Return messages
   Dim pdus() As Object
   Dim r As Reflector
   pdus = In.GetExtra("pdus")
   If pdus.Length > 0 Then
      Dim messages(pdus.Length) As Message
      For i = 0 To pdus.Length - 1
         r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
            Array As Object(pdus(i)), Array As String("[B"))
         messages(i).Body = r.RunMethod("getMessageBody")
         messages(i).Address = r.RunMethod("getOriginatingAddress")
                                    Main.SucheTextSMS=r.RunMethod("getMessageBody")
      Next
   End If
   Return messages
End Sub
in activity_Main
B4X:
Sub MarkerSetzenAusSMS
Dim bez,SMStemp As String

SMStemp=SucheTextSMS.Replace("(My Text send from app)","")
Dim LL,LT As List
Dim l1,l2 As String
Dim sf As StringFunctions
sf.Initialize
LT = sf.Split(SMStemp,"-")
SMStemp=LT.Get(0)
bez=LT.Get(1)

LL = sf.Split(SMStemp,",")
l1=LL.Get(0)
l2=LL.Get(1)

gmap.AddMarker2(l1,l2, bez&" ("&l1&","&l2&")",gmap.HUE_ORANGE)
            Dim CameraPosition1 As CameraPosition
   CameraPosition1.Initialize(l1,l2, 15)
   gmap.AnimateCamera(CameraPosition1)
           
Sms.TextSMSvorhanden=False
  
End Sub
 
Last edited:

Vitt61

Member
Licensed User
Longtime User
The #StartAtBoot: True attribute adds an item to the manifest file which tells the OS that this service should be started after boot completes.

I test the app auto start but I receive this error:
B4A version 4.30
Parsing code. Error
Error parsing program.
Error description: Attribute not supported: startatboot
Occurred on line: 22
#StartAtBoot: True
In my # list I don't have "StartAtBoot"
 
Top