Reputation: 100358
I have an Activity that starts a Service.
In my Activity:
startService(new Intent(this, MyService.class));
In my Service, onStart():
/* Show notification */
int icon = R.drawable.icon;
tickerText = getResources().getString(R.string.app_name);
long when = System.currentTimeMillis();
contentTitle = getResources().getString(R.string.app_name);
contentText = getResources().getString(R.string.running);
Intent notificationIntent = new Intent(this, activityClass).setAction(Intent.ACTION_MAIN).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT;
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification);
startForeground(0, notification);
The service has a timer that does some work every 3-4 minutes, and broadcasts info to the Activity if possible. When the Activity is closed, the Service should keep doing its work.
When I run the app, and go back to the homescreen, the Service keeps running. But after a while I get these logcat messages, and the service stops:
07-03 16:55:09.440: INFO/ActivityManager(583): Process com.myapp (pid 11665) has died. 07-03 16:55:09.440: WARN/ActivityManager(583): Scheduling restart of crashed service com.myapp/.MyService in 5000ms
How can I prevent this?
Upvotes: 3
Views: 5843
Reputation: 4319
The provided answers didn't help me. But I found a really easy way to do this, add in the Manifest
<service android:name="..." android:process=":remote" />
http://developer.android.com/guide/topics/manifest/service-element.html
Just testing at the moment, my Activity got already killed, but my service is running.
Upvotes: 7
Reputation: 1006584
When I run the app, and go back to the homescreen, the Service keeps running. But after a while I get these logcat messages, and the service stops... How can I prevent this?
You don't, generally speaking.
Services are not designed to run forever. Too many developers have started services and never stopped them. And, in your case, they do so for no good reason. Having a service hang around in memory just watching the clock tick is wasteful, and it is particularly because developers do things like this that Android "crashes" services after they stick around too long.
In your case, please switch to use AlarmManager
, probably in conjunction with an IntentService
, so the service shuts down on its own accord when there is no more work to be done.
Upvotes: 2
Reputation: 41858
You will want to look at having your service run as a Remote Service
, so that it runs in its own process.
A blog about remote services: http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-part-9.html
An example of a sample one: http://about-android.blogspot.com/2010/02/android-remote-service-sample.html
Upvotes: 1