DEVANG SHARMA
DEVANG SHARMA

Reputation: 2662

Keep Service Alive which Starts from Broadcast Receiver

I have a major Issue,

I make a Broadcast receiver which apply on the Device boot up, so i need to start a new service for performing long-running operation,

So in the onReceive() method of Broadcast Receiver I make a new Intent and by this Start a new service,

Now my problem is that this Service executes only for short time, as soon as the onRecieve() method finish it process is also finished and my Service is also stops with the finishing of Receiver process.

So how I can do this, to keep alive the Process of Service which starts from the BroadcastReceiver.

Upvotes: 1

Views: 1012

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

Now my problem is that this Service executes only for short time, as soon as the onRecieve() method finish it process is also finished and my Service is also stops with the finishing of Receiver process.

That would only occur if you are calling stopService(), or the service is calling stopSelf(). The service has an independent lifecycle from the BroadcastReceiver. It will not even be started before onReceive() ends.

but in the Service I use the Separate Thread, but this thread is also stopped.

That will only occur if you are stopping the thread yourself. Android does not know about threads you create.

Now, eventually, your app's process will be terminated. With a running service, this could take anywhere from 30 minutes to a couple of days, depending on what else is all going on with the device, whether you are using startForeground(), etc. Once the process is terminated, everything goes away, background thread and all.

Upvotes: 3

Related Questions