Android Question menu to close app , services not stop

joe.dai

Member
HI

How do I grab the menu leaving event (forced shutdown program) to close all running services ?

menu to close app , services not stop

2.png
 

joe.dai

Member
Only kiosk apps can disable this button.

It will raise the Activity_Pause or B4XPage_Background events.

I have used the following, the Activity_Pause event, menu button or home button will be triggered before forcibly closing ,
At this time, the APP has not been forcibly closed!

B4X:
Sub Activity_Pause (UserClosed As Boolean)
  If UserClosed = False Then
           StopService(xxx)
  Else
           Activity.Finish()
  End If
End Sub

How can I execute StopService(XXX) after APP is forcibly closed ??
Because now my APP is forced to close and it crashes.

Thanks Erel
 
Upvote 0

joe.dai

Member
I don't understand the purpose of this code. Looks (completely) wrong.


Post the error message.

The purpose of my APP is only needed. When the user closes the APP with the menu button, the service can be stopped together.
Whether the method used is incorrect, please guide again

Thank you Erel

3.png
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
The purpose of my APP is only needed. When the user closes the APP with the menu button, the service can be stopped together.
Whether the method used is incorrect, please guide again

Thank you Erel

View attachment 103079
Hi,

Here's how I stoped my app from running in the background. Because the user wanted a choice to stop the app from running in the background or to press the home button and keep it running because it's a music streaming app, I put this coding in a button click event. You can use similar coding on your Activity_Pause sub routine. I stopped a service module and the starter module. I may be using the wrong approach but it seemed to work for me.

Coding in my main module:
B4X:
Sub ImageViewExitAndStop_Click

    CallSubDelayed(ServiceModule, "CancelThisService")
    StopService(Starter)
    
    ToastMessageShow("App NOT running in the background.", True)
    
    StopService(ServiceModule)
    
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "")
    i.AddCategory("android.intent.category.HOME")
    i.Flags = 0x10000000
    StartActivity(i)
End Sub

The last 6 lines I got from a post from the B4A forum and have been using them every time I exit my apps.

This coding is from a service module I named ServiceModule. I created it so the app will run in the background and make it hard for Android to stop the app from running, but I have a sub routine to stop it if the user wants to do that through the exit and stop button on the app:
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.

    Private nid As Int = 1
    Dim lock As PhoneWakeState
End Sub

Sub Service_Create

    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    lock.PartialLock
End Sub

Sub Service_Start (StartingIntent As Intent)

    Service.StartForeground(nid, CreateNotification("Tapping this notification will open the app."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
End Sub

Sub Service_Destroy

End Sub

' Misc. sub routines.
'--------------------
Sub CreateNotification (Body As String) As Notification

    Dim notification As Notification

    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Now running in the background.", Body, Main)

    Return notification
End Sub

Sub CancelThisService
    lock.ReleasePartialLock
    CancelScheduledService(Me)
    Service.StopForeground(nid)
End Sub

If you want to see the coding in this app in action, you can download it from Google at: VIFM app

I'm also going to be uploading it to the Huawei app store.

I hope this helps you.
 
Upvote 0

joe.dai

Member
Please post it as text.

I added the following code to the starter and solved it

Thank you Erel

starter service :
B4X:
Sub Service_TaskRemoved
    StopService(xxx)
    'This event will be raised when the user removes the app from the recent apps list.
End Sub
 
Upvote 0

joe.dai

Member
Hi,

Here's how I stoped my app from running in the background. Because the user wanted a choice to stop the app from running in the background or to press the home button and keep it running because it's a music streaming app, I put this coding in a button click event. You can use similar coding on your Activity_Pause sub routine. I stopped a service module and the starter module. I may be using the wrong approach but it seemed to work for me.

Coding in my main module:
B4X:
Sub ImageViewExitAndStop_Click

    CallSubDelayed(ServiceModule, "CancelThisService")
    StopService(Starter)
   
    ToastMessageShow("App NOT running in the background.", True)
   
    StopService(ServiceModule)
   
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "")
    i.AddCategory("android.intent.category.HOME")
    i.Flags = 0x10000000
    StartActivity(i)
End Sub

The last 6 lines I got from a post from the B4A forum and have been using them every time I exit my apps.

This coding is from a service module I named ServiceModule. I created it so the app will run in the background and make it hard for Android to stop the app from running, but I have a sub routine to stop it if the user wants to do that through the exit and stop button on the app:
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.

    Private nid As Int = 1
    Dim lock As PhoneWakeState
End Sub

Sub Service_Create

    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    lock.PartialLock
End Sub

Sub Service_Start (StartingIntent As Intent)

    Service.StartForeground(nid, CreateNotification("Tapping this notification will open the app."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
End Sub

Sub Service_Destroy

End Sub

' Misc. sub routines.
'--------------------
Sub CreateNotification (Body As String) As Notification

    Dim notification As Notification

    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Now running in the background.", Body, Main)

    Return notification
End Sub

Sub CancelThisService
    lock.ReleasePartialLock
    CancelScheduledService(Me)
    Service.StopForeground(nid)
End Sub

If you want to see the coding in this app in action, you can download it from Google at: VIFM app

I'm also going to be uploading it to the Huawei app store.

I hope this helps you.

Thank you for your reference resources,
 
Upvote 0
Top