Iam running this procedure to download some data and insert them into the database. The total procedure takes around 5 minutes. I noticed that while downloading, when the phone locks the screen and open it after 5 minutes, it will still downloading. It seems when locked download procedure slows down. Is there any explanation?
The execution time also slows down when pressing home button and becomes a background process, not only when sreen locks.
Thank you
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase sInstance;
@VisibleForTesting
public static final String DATABASE_NAME = "Database_db";
public abstract CustomerDao repoCustomer();
public static AppDatabase getInstance(Context context) {
if (sInstance == null) {
synchronized (AppDatabase.class) {
if (sInstance == null) {
sInstance = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME).build();
}
}
}
return sInstance;
}
public void downloadCustomers(final String table){
executors.diskIO().execute(new Runnable() {
@Override
public void run() {
//download data and insert into database.
});
}
}
I believe it is something related with power management. Have you tried using a wake lock?
To test if that is your problem, simply add android:keepScreenOn="true" to the layout of the activity where the thread is started.
If it solves the problem and you don´t need the screen on, consider reading this thread:
https://developer.android.com/training/scheduling/wakelock
To aquire a wakelock you must add this to your manifest:
And set manually the wake lock:
To manually release it, you can do it with:
Also, from the same source and it seems to me that this can be applied to your problem, check this out:
Before adding wakelock support to your app, consider whether your app's use cases support one of the following alternative solutions:
"If your app is performing long-running HTTP downloads, consider using DownloadManager. If your app is synchronizing data from an external server, consider creating a sync adapter. If your app relies on background services, consider using JobScheduler or Firebase Cloud Messaging to trigger these services at specific intervals."
Hope it helps.