In my aplication I have service module and three activities.
In service module I detect whether bluetooth module is on.
If user or another aplication turns off BT (when my aplication is running on). My aplication should be closed as well.
I wonder what should be closed at the beginning.
service module or aplication.
It is my code to detect if BT was turned off
B4X:
Sub admin_StateChanged (NewState As Int, OldState As Int)
If (IsPaused(Main) = False Or IsPaused(Parameters) = False Or IsPaused(calibration)=False) And (NewState = admin.STATE_OFF) Then
Common.connected = False
Log ("BT off")
what should be here ?
End If
End Sub
If you're showing an Activity at that time, you can close that Activity. If you have scheduled Service starts, you can cancel them. You shouldn't try to end the process; that is the OS's job.
For example current Activity is not Main but another.
and in admin_State .... I have the following code
B4X:
Sub admin_StateChanged (NewState As Int, OldState As Int)
If (IsPaused(Main) = False Or IsPaused(Parameters) = False Or IsPaused(calibration)=False) And (NewState = admin.STATE_OFF) Then
Common.connected = False
Log ("Off")
Service.StopForeground(0)
CallSub(another,"Finish")
End If
End Sub
and in another activity I have
B4X:
Sub Finish
Activity.finish
End Sub
The current Activity will be finished and my aplication returns to Main Activity - the aplication will not be closed
You can think of Services as working either in background mode or in foreground mode. If they work in background mode, the OS might kill them occasionally. If they work in foreground, the OS won't kill them. ServiceStart starts a Service in background mode because, most often, it is called from an Activity (all Activities work in foreground mode).
If the aplcation starts first time all Callsub where is called sub from service module are not exectuing (but it is only when the aplication is run first time)
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("1")
Activity.Title = "pH metr"
If FirstTime = True Then
StartService(BT)
Timer1.Initialize("Timer1",100 )
End If
End Sub
B4X:
Sub Activity_Resume
CallSub(BT,"BT_Status")
End Sub
The above callsub does not work. If the aplication starts first time
B4X:
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(0,Null)
End Sub