question about service

marcick

Well-Known Member
Licensed User
Longtime User
Hi.
My project consist of two parts.
First part is a background service module that capture incoming SMS and store them in a file. This service must be always running, I can't loose any incoming SMS.
Second part is an application that read the file created by the service and do other things. The application, as any other application, can be running or not, never mind.

Actualy I do this with two different packages, one for the service and one for the application.

I would like to distribute just one pachage, that include the service also. So that, once the user start the application the first time after installing it, the service is started and never die, also if the application is closed.
Is it possible ?
Marco
 

kolbe

Active Member
Licensed User
Longtime User
The service will have to have an activity as well, I don't think you can just have a service. The activity can remain paused however. If in the service you use service.startforground the whole process should be kept alive by the OS. You will just need some kind of an event to trigger the start of the service each time an sms arrives. Alternatively you could schedule the service to periodically start and check for new sms.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
yes, actually I have an activity that do nothing, just start the service. And a second separate activity that can run or not and do other things.
So my full application consists of two separate packages to be installed. I would like to distribute the application as a single package ....
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
You can do this with one package having an activity module and a service module. I don't see why you need two packages/apps for this.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Ok, let's start from the point I'm a beginner ...
It seems to me that with a single package, if the user stops the application also the service stops running.
Am I wrong ?
Marco
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
That's true yes, but with two apps running, that does not guarantee that your user won't stop them either! Remember, Services show up in Settings too and can be stopped by the user.

And I don't think you can have just a Service Module app, I think you have to have an Activity attached to it, so why not use that activity vs creating a second app just to get the activity?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Do not use a Timer in your Service module to check for SMS messages, use the StartServiceAt method as Kolbe mentioned. A timer will eventually fail.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
The app that contain the service is hidden, I mean the user must go in Settings,applications, running tasks and stops it. Not easy to do for mistake.

I have this code:

Sub Activity_Create(FirstTime As Boolean)

StartService(prova)
Activity.Finish

End Sub


The main application instead, it's enough that the user press the Home or Back simbol and it's stopped.

p.s.
I do not use a timer, I use SMSinterceptor that generate an event when receive sms.
Also I'm not sure I need StartserviceAt. I just start the service once and it runs forever. It seems to me ....
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I should ask all of you to properly read about services here: Services

In particular make note:
So by only using activities it is not possible to run any code while your application is not visible.
Services life cycle is (almost) not affected by the current visible activity. This allows you to run tasks in the background.

Sub Service_Start is called each time you call StartService (or StartServiceAt). When this subs runs the process is moved to the foreground state. Which means that the OS will not kill your process until this sub finishes running. If you want to run some code every couple of minutes / hours you should schedule the next task with StartServiceAt inside this sub.

Running a long operation. For example downloading a large file from the internet. In this case you can call Service.StartForeground (from the service module). This will move your activity to the foreground state and will make sure that the OS doesn't kill it. Make sure to eventually call Service.StopForeground.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Ok guys,
Thank you, each contribute is useful to learn more and more. but we are going off road ...
I have read again my initial post and seems to me the question is clear....
 
Upvote 0

touchsquid

Active Member
Licensed User
Longtime User
We have an app that uses several apks, one of which is a service. We include the secondary apks in the files section of the main app. We check the version number, and if outdated, install a new one. Code below. With variations on this you can package an unlimited number of apks inside one.

In Activity resume:

If TSWidgetVersion < 3 Then
Msgbox("Updating Launcher Widget.", "TouchSquid Update")
APKupdate.UpdateAPK("TSwidget.apk")
End If

Sub UpdateAPK(fn As String)
Try
Dim iIntent As Intent
File.Copy(File.DirAssets,fn, File.DirDefaultExternal,fn)
iIntent.Initialize(iIntent.ACTION_VIEW, "file:" & File.DirDefaultExternal & "/" & fn)
iIntent.SetType("application/vnd.android.package-archive")
StartActivity(iIntent)
Catch
End Try
End Sub

Sub TSWidgetVersion() As String
Try
Dim PM As PackageManager
Return PM.GetVersionCode("TS.Startup.Widget")
Catch
Return "0"
End Try
End Sub
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Very interesting, I'll do some experiments on this.
Anyway I have realized that if the main application start the service module and the service module is declared as "start at boot", the first time the user launch the appplication the service is started and never ends, also when the main application is terminated or the device is rebooted. This is what I need.

But your example is very interesting and gives me other ideas ....
Many thanks
Marco
 
Upvote 0
Top