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:
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:
#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.