Android Question Create a pure service without any GUI

Alessandra Pellegri

Active Member
Licensed User
Longtime User
I would like to create a service that make the phone ringing when a bluetooth device goes out of range.

The point is this: how can I make the service running without any visible gui ?

I tried this code:
B4X:
#Region  Project Attributes
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: 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.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Log("OK_1")
   Activity.LoadLayout("Main")
   CallSubDelayed("","CloseActivity")
   StartService(Trova)
End Sub

Sub CloseActivity
   Activity.Finish 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

B4X:
#Region  Service Attributes
   #StartAtBoot: False
   
#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 BTAdmin As BluetoothAdmin
   Dim BTSerial1 As Serial
   Dim BTFoundDevices As List
   Type BTNameAndMac (Name As String, MAC As String)
   Dim connectiontimer As Timer
   Dim found As Boolean=False
   Dim countdown As Int=0
   Dim MP As MediaPlayer
End Sub

Sub Service_Create
   BTAdmin.Initialize("BTAdmin")
   BTSerial1.Initialize("BTSerial1")
   Log("Start")
End Sub

Sub Service_Start (StartingIntent As Intent)
   If countdown<20 Then countdown=countdown+1
   If found=True Then countdown=0
   Log(countdown)
   If countdown>=6 Then
       MP.Initialize
       MP.Load(File.DirAssets, "Suono.wav")
       MP.Play
   End If
   found=False
   BTFoundDevices.Initialize
   BTAdmin.StartDiscovery
End Sub

Sub Service_Destroy

End Sub

Sub BTAdmin_DiscoveryFinished
   StartServiceAt("Trova",DateTime.Now + 20000,True)
End Sub

Sub BTAdmin_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ":" & MacAddress)
   If MacAddress="D1:00:00:01:FF:FF" Then
       found=True
   End If
End Sub

This works but if I press the Overview I can still see the app.
Anyway after about 1 minutes since the phone is sleeping the debugger of B4A disconnects. I don't know if it is just the debugger to disconnect itself or the app dies.
 

MarkusR

Well-Known Member
Licensed User
Longtime User
i would try this order

B4X:
Sub Activity_Create(FirstTime As Boolean)
   StartService(Trova)
   Activity.Finish
End Sub
 
Upvote 0

Alessandra Pellegri

Active Member
Licensed User
Longtime User
In this forum I found that Activity.Finish cannot stay in Activity_Create.
Anyway I tried to put CallSubDelayed("","CloseActivity") after Activity.LoadLayout("Main") and It doesn't chenge anything.
 
Upvote 0

Alessandra Pellegri

Active Member
Licensed User
Longtime User
I found in this forum that Activity.Finish cannot stay directly in Activity_Create but it shoud be called with CallSubDelayed.
Anyway I tried to put StartService(Trova) before CallSubDelayed("","CloseActivity") and it doesn't change anything.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
where you press Overview? i guess outside this app, true?

and this?
B4X:
Sub Activity_Create(FirstTime As Boolean)
   StartService(Trova)
End Sub
Sub Activity_Resume

   Activity.Finish
 
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I would like to create a service that make the phone ringing when a bluetooth device goes out of range.

The point is this: how can I make the service running without any visible gui ?

I tried this code:
B4X:
#Region  Project Attributes
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: 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.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Log("OK_1")
   Activity.LoadLayout("Main")
   CallSubDelayed("","CloseActivity")
   StartService(Trova)
End Sub

Sub CloseActivity
   Activity.Finish
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

B4X:
#Region  Service Attributes
   #StartAtBoot: False
  
#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 BTAdmin As BluetoothAdmin
   Dim BTSerial1 As Serial
   Dim BTFoundDevices As List
   Type BTNameAndMac (Name As String, MAC As String)
   Dim connectiontimer As Timer
   Dim found As Boolean=False
   Dim countdown As Int=0
   Dim MP As MediaPlayer
End Sub

Sub Service_Create
   BTAdmin.Initialize("BTAdmin")
   BTSerial1.Initialize("BTSerial1")
   Log("Start")
End Sub

Sub Service_Start (StartingIntent As Intent)
   If countdown<20 Then countdown=countdown+1
   If found=True Then countdown=0
   Log(countdown)
   If countdown>=6 Then
       MP.Initialize
       MP.Load(File.DirAssets, "Suono.wav")
       MP.Play
   End If
   found=False
   BTFoundDevices.Initialize
   BTAdmin.StartDiscovery
End Sub

Sub Service_Destroy

End Sub

Sub BTAdmin_DiscoveryFinished
   StartServiceAt("Trova",DateTime.Now + 20000,True)
End Sub

Sub BTAdmin_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ":" & MacAddress)
   If MacAddress="D1:00:00:01:FF:FF" Then
       found=True
   End If
End Sub

This works but if I press the Overview I can still see the app.
Anyway after about 1 minutes since the phone is sleeping the debugger of B4A disconnects. I don't know if it is just the debugger to disconnect itself or the app dies.
Hi, Alessandra,
Obviously if you start the App even if you close it in the overview will always appear in the App, except if you kill it personally from the TaskManager.

If you do not want to see the App from the overview, start the service when the smartphone starts, and if necessary use the App to close the service or for other settings

B4X:
#StartAtBoot: True
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
You should call Service.StartForeground and acquire a partial lock with PhoneWakeState.
hi erel, in which sub do we need place this commands? is notification.Number the StartForeground(ID ?
 
Upvote 0

Alessandra Pellegri

Active Member
Licensed User
Longtime User
OK, now it works.
I am using Service.StartForeground.

If a I correctly understand using StartForeground I can use Timer instead StartServiceAt. Is it ?
 
Upvote 0
Top