1

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.

DEVANG SHARMA
  • 2,662
  • 5
  • 33
  • 52
  • I'm not sure if this matters but are you using a separate thread for your service? – Noel Oct 06 '11 at 05:44
  • http://stackoverflow.com/questions/3696082/how-to-schedule-my-android-app-to-do-something-every-hour Follow the two links and try out the example. – Spencer Oct 06 '11 at 05:58
  • No not use the separate thread, but in the Service I use the Separate Thread, but this thread is also stopped. – DEVANG SHARMA Oct 06 '11 at 06:08

1 Answers1

3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But Then why this service terminates, If I call the startService() inside the activity then it not terminates – DEVANG SHARMA Oct 06 '11 at 07:17
  • can you please give me a code for start activity from BroadcastReceiver or Service class, I use the following code but error comes. Intent i=new Intent(this,MyActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); Error is the Flag error, it says cant start activity without activity context – DEVANG SHARMA Oct 06 '11 at 07:18
  • please give me a reply I really need this – DEVANG SHARMA Oct 06 '11 at 07:20
  • So even when using Service, there is a chance that Android will kill it without user intervention? If so, how does the other apps work if they have some running instructions in the background for a long time? – Wissem Oct 02 '12 at 09:17