Non-terminating service

madSac

Active Member
Licensed User
Longtime User
I have nearly completed my application but I am still facing an issue.
I have a service which always runs in background, searching much in forum I used method startserviceat().
But I am not satisfied.
I tried using method startforeground with a notification icon but it was always cleared out whenever I cleaned using estaskmanager. I compared my service with whatsapp, it was never terminated except when I killed it manually...and there service uptime was much greater than mine even I started my service with whatsapp.
So can anyone provide a better solution I would really appreciate.

I just want to make a service always alive just like whatsapp messaging service.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Based on this thread: android - how does whatsapp service gets restarted even if i force stop app? - Stack Overflow Whatsapp uses the standard push framework to receive messages.

There are other workarounds (which are not 100% reliable). You can create a static intent filter that listens to a relatively common message. This will allow you to restart your process if it was killed by a task manager.

It is not possible to create a process that cannot be killed by a task manager.
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
Based on this thread: android - how does whatsapp service gets restarted even if i force stop app? - Stack Overflow Whatsapp uses the standard push framework to receive messages.

There are other workarounds (which are not 100% reliable). You can create a static intent filter that listens to a relatively common message. This will allow you to restart your process if it was killed by a task manager.

It is not possible to create a process that cannot be killed by a task manager.


ok I understood I will find a way using intent.

The main problem that startserviceat method is not reliable as when I started thread the service was running and now its terminated.

Can you tell why process is terminated even if I use startforeground ?
I tried your sample and it was terminated where as other similar services were not terminated like uc download manager.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Whether the service is kept running or not depends on the task manager. Service.StartForeground is a standard Android API.
You can search Google. You will see that there are many similar discussions about task managers and services. There is no simple way to keep a service running if the user is using a task manager.
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
Whether the service is kept running or not depends on the task manager. Service.StartForeground is a standard Android API.
You can search Google. You will see that there are many similar discussions about task managers and services. There is no simple way to keep a service running if the user is using a task manager.

I haven't killed a process individually I just used optimizer option which kills all processes.My service was killed but whatsapp and ucdownload services we're running uninterrupted I just want to achieve that.
Take a look Android Service Tutorial
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
I not only want to download....but I want to run a service which always runs....it also listens to c2dm
 
Upvote 0

qnw4ts7pge

Member
Licensed User
Longtime User
So here is my solution...

I have a service that I restart in Service_Start with StartServiceAt......now+10000 (every 10 seconds)

I don't know if there are any drawbacks yet.

I have ONE feature that I don't want to persist across reboots. When rebooted, it should default to "off", but I don't have any way to tell if the Service_Create was invoked by a boot, or a StartServiceAt?

Is there an UpTime function or similar?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
You can detect if the service was (auto) started at boot using this code:

B4X:
Sub Service_Start (StartingIntent As Intent)

   If StartingIntent.Action="android.intent.action.BOOT_COMPLETED" Then
      ' service was auto started at boot
   Else
      ' service was not auto started at boot
   End If
   
End Sub

Martin.
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
Is there an UpTime function or similar?

This gives you uptime of your application & shows the drawback of startserviceat....sometimes you will get uptime very low as application is closed & started again..variables are reseted !
B4X:
Dim os As OperatingSystem
log(os.ElaspedCPUTime)
 
Upvote 0
Top