Android Question How to close aplication properly

Arturs

Member
Licensed User
Longtime User
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
 

Roycefer

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
The apliacation should be closed only when one of them is showed. In another case it does not matter.

My service module works all the time Service.StartForeground(0,Null)

so only Add ExitAplication in service module ?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Erel recommends against using
B4X:
ExitApplication
Instead, try calling
B4X:
Activity.Finish
from inside the active Activity and
B4X:
Service.StopForeground(0) 'or whatever your Notification ID is
from inside the Service. This will allow the OS to kill your process when it wants.
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
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
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
the aplication will not be closed
So you would have to call a sub inside the Main activity which will finish it too when the UserClosed is True in the current activity
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
Now. Everything is clear but I have another problem

Why My service starts 2 times


B4X:
Sub Service_Start (StartingIntent As Intent)
              
End Sub

In debug mode I watch that aplication goes into the above SUB 2 times
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
I call service start only once in Activity_Create
B4X:
If FirstTime = True Then            
        StartService(BT)   
Sub

but In log windows I observe that the servicestart was called two times. It is only place where I call ServiceStart

second problem

I have read the article https://www.b4x.com/android/forum/threads/service-modules.7542/ and I analyzed example codes but I still
do not know difference between ServiceStart/Stop which is Activity Create and ServiceStart/Stop.foreground.


It seems that ServiceStart in Activity Create only start service but without specific mode or default

but Service.StartForeground in Service module set the mode of working service module

Of course I can "follow" for your example code and do it like you but I would like to understand it

If anywhere it was explained please give me URL.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
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).
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Yes, that is correct. Make sure to call
B4X:
Service.StopForeground
when you no longer need the Service to be in the foreground.
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
Ok. Thank you for information.
Today I have another problem, my aplication works well in Debug mode when I released the aplication It does not work

What are differences between Debug and Relase but I think it should work too

I mean 90 % percent works but 10% does not work ;/;/
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
Can I use some subs from Service module before finishing activity_resume ?
I think No but Is it a solution ?
 
Last edited:
Upvote 0

Arturs

Member
Licensed User
Longtime User
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)
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
B4X:
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


Service_Create is empty
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…