Android Example [Example] Creating a sticky service - long running background tasks

Please see the original post:

About sticky service - long running background tasks:
https://www.b4x.com/android/forum/t...-long-running-background-tasks.27065/#content

About Starter service module:
https://www.b4x.com/android/forum/threads/starter-service-consistent-single-entry-point.57599/

-------------------------------------
In this code we can find:

- Start At Boot
- Start Foreground
- Stop Foreground

- StartActivity

- Call sub: CallSubDelayed
- Transport data: CallSubDelayed2 and CallSubDelayed3

Starter (Service Module):
(added sub KillService)

B4X:
#Region  Service Attributes
   #StartAtBoot: False
   #ExcludeFromLibrary: 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 Service_Create
   'This is the program entry point.
   'This is a good place to load resources that are not specific to a single activity.
 
End Sub

Sub Service_Start (StartingIntent As Intent)
 
End Sub

Sub Service_TaskRemoved
   'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

Sub Service_Destroy

End Sub

Sub KillService
   StopService(Me)
End Sub

Service2 (Service Module):

B4X:
#Region  Service Attributes
   #StartAtBoot: False
   #StartCommandReturnValue: android.app.Service.START_STICKY
#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 test As Notification
   Dim IDservice As Int = 1
   Dim timer1Service As Timer
End Sub

Sub Service_Create
   'This is the program entry point.
   'This is a good place to load resources that are not specific to a single activity.
   test.Initialize
   test.Icon = "image1name"    ' \Objects\res\drawable\image1name.png [read only to stay]
   test.SetInfo("title","body",Main)
   'Service.StartForeground(IDservice,test)
 
   timer1Service.Initialize("timer1Service", 10000)
End Sub

Sub Service_Start (StartingIntent As Intent)
   timer1Service.Enabled = True
End Sub

Sub Service_TaskRemoved
   'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

Sub Service_Destroy

End Sub

Sub timer1Service_tick
   ToastMessageShow("Hello word!", True)
End Sub

Sub CreateService
   Service.StartForeground(IDservice,test)
 
   ' send "Started" to sub "ShowMxgbox" of Main (Activity Module)
   CallSubDelayed2(Main, "ShowMxgbox", "Started")
End Sub

Sub KillService
   timer1Service.Enabled = False

   Service.StopForeground(IDservice)
 
   ' send "Stopped" to sub "ShowMxgbox" of Main
   CallSubDelayed2(Main, "ShowMxgbox", "Stopped")
 
   StopService(Me)
End Sub

Sub SubOne(value1 As Int)
   Dim str1 As String = value1
   ' send "value1" to sub "ShowMxgbox" of Main
   CallSubDelayed2(Main, "ShowMxgbox", str1)
End Sub

Sub SubTwo(value1 As Int, value2 As String)
   ' send "value1 and value2" to sub "ShowMxgbox" of Main
   CallSubDelayed3(Main, "ShowMxgboxValue2", value1, value2)
End Sub

StartAtBoot (Service Module):

B4X:
#Region  Service Attributes
    #StartAtBoot: 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 Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    StartActivity(Main)
End Sub

Sub Service_Destroy

End Sub

Main (Activity Module):

B4X:
#Region  Project Attributes
   #ApplicationLabel: Service 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.
 
   Private Button0 As Button
   Private Button1 As Button
   Private Button2 As Button
   Private Button3 As Button
   Private Button4 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Layout1")
   StartService(Service2)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button0_Click
   CallSubDelayed(Service2, "CreateService")
End Sub

Sub Button1_Click
   'call sub KillService of Service2 (Service Module)
   CallSubDelayed(Service2, "KillService")

   'call sub KillService of Starter (Service Module)
   CallSubDelayed(Starter, "KillService")
End Sub

Sub Button2_Click
   ExitApplication
End Sub

Sub ShowMxgbox(text1 As String)
   Msgbox(text1, "Service")
End Sub

Sub ShowMxgboxValue2(num1 As Int, text1 As String)
   Msgbox(num1, text1)
End Sub

Sub Button3_Click
   ' send "2016" to sub "SubOne" of Service2
   CallSubDelayed2(Service2, "SubOne", 2016)
End Sub

Sub Button4_Click
   ' send "16 and text" to sub "SubTwo" of Service2
   CallSubDelayed3(Service2, "SubTwo", 16, "text")
End Sub
 

Attachments

  • Services_example.zip
    9 KB · Views: 648
  • Screenshot_2016-11-25-12-23-53.png
    Screenshot_2016-11-25-12-23-53.png
    18.9 KB · Views: 675
  • Screenshot_2016-11-25-12-35-40.png
    Screenshot_2016-11-25-12-35-40.png
    77.4 KB · Views: 701
  • Screenshot_2016-11-25-12-37-41.png
    Screenshot_2016-11-25-12-37-41.png
    46.7 KB · Views: 670
Last edited:
Top