fypfyp
fypfyp

Reputation: 206

How to stop application/service from destroying?

I have an application that has a single screen with a "start logging" button, which starts a service. After it is pressed, the button changes to a "stop logging" button and stops the service onClick. This service essentially grabs some data every minute and writes it to a couple of SQLite tables.

The application is designed to be opened, start logging.. and then minimised and left in the background to run. However, the problem is that after a period of time, Android destroys/stops the process/service/activity so that it no longer logs data, and when the application is reopened, the screen is reset so that it says "start logging" again instead of "stop logging" because the application has been restarted.

I'm looking for the application/service to be able to run in the background and collect data for as long as it wants, and for the activity to remain open or in a state where when it is opened, I can stop logging rather than having the screen reset. Basically, the application is minimized forever when logging and not closed.

Thanks for any help or advice.

Upvotes: 0

Views: 157

Answers (3)

Marcus Ataide
Marcus Ataide

Reputation: 7530

You should implements Foreground.

To request that your service run in the foreground, call startForeground(). This method takes two parameters: an integer that uniquely identifies the notification and the Notification for the status bar. For more info, go:

http://developer.android.com/guide/components/services.html#Foreground

Upvotes: 1

psykhi
psykhi

Reputation: 2981

Problem is that when the OS gets out of RAM, it will destroy your service if it's in the background. That's why services that can't be stopped (like mp3 playing) are foreground service. So you could make your service run in the foreground if you absolutely want to keep it alive. I know it's probably not what you want, but it feels like you don't have much of a choice. Putting a service to the foreground is the way to tell Android that your service must not be killed I think.

Upvotes: 0

ahodder
ahodder

Reputation: 11439

What you are looking for is a sticky startService. This will allow the service to run until the system runs out of memory. For more info on Services, look here.

Upvotes: 0

Related Questions