Yep. Your problem is indeed due to device running low on memory.
When resources turn low Android simply disposes of running services, such as yours.
What happen next depends on you
Declare as sticky
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
Adding the above to your service will force Android to restart it after killing, once resources
go up again .
If a running service is crucial for your system and you do not want it auto-killed under any
circumstance (tip: most of the time this is not what you want), you can do it by declaring
it to be a foreground service
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("ticker text").setContentTitle("title text").setContentText("content")
.setWhen(System.currentTimeMillis()).setAutoCancel(false)
.setOngoing(true).setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendIntent);
Notification notif = builder.build();
notif.flags |= Notification.FLAG_NO_CLEAR;
startForeground(NOTIF_ID, notif);
Final note: IntentService spawns only a single thread on which all requests are executed.
This works fine in many cases. But, when it comes to IO-bound execution, you will usually get
better performance using multiple threads.
So, and only if performance is an issue, consider using e.g. a multi-thread pool for the job:
ThreadFactory threadFactory = Executors.defaultThreadFactory();
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(
MIN_NUM_THREADS, MAX_NUM_THREADS, 10, TimeUnit.SECONDS, ...);
...
executorPool.execute(new MyWorkerThread(params));
Where the first two params received by ThreadPoolExecutor constructor set min and max number
of concurrently active threads.