Reputation: 5145
I am developing an app that uses intent services to backup some files. The app works fine and does not crash. However, when I start an app in background the app will close sometimes when it runs for two days or sometimes in one night only.
Is it because that device is low on memory and my app doesn't close correctly?
Is there any intent i.e I handle this condition in my app and I will show the reason to my user why the last process stopped.
Upvotes: 3
Views: 2742
Reputation: 5867
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.
Upvotes: 3
Reputation: 8338
You can use these to measure the memory.
Gdx.app.getJavaHeap();
Gdx.app.getNativeHeap();
Since you app runs on the background, maybe you could save the values for later study?
Upvotes: 1