Reputation: 4737
I'm trying to use DownloadManager to load multiple files (100+), while enqueuing file urls app freeze and while downloading files app is really lag.
DownloadManager downloadManager = ( DownloadManager ) context.getSystemService( Context.DOWNLOAD_SERVICE );
DownloadManager.Request request;
long[] ids = new long[ tiles.size() ];
int cpt = 0;
for ( MapUtils.Tile tile : tiles )
{
String path = "/tiles/" + provider.getId() + "/" + tile.getZ() + "/" + tile.getX() + "/" + tile.getY() + ".jpg";
File file = new File( context.getExternalFilesDir( null ) + path );
Log.d( "MapUtils", "download " + file.getPath() + " --- " + file.exists() );
if ( file.exists() ) file.delete();
request = new DownloadManager.Request( Uri.parse( tile.getUrl( provider.getUrl() ) ) );
request.setAllowedNetworkTypes( DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE );
request.setAllowedOverRoaming( false );
request.setVisibleInDownloadsUi( false );
request.setTitle( tile.getUrl( provider.getUrl() ) );
request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_HIDDEN );
request.setDestinationInExternalFilesDir( context, null, path );
ids[ cpt ] = downloadManager.enqueue( request );
cptt+
}
for loop freeze app here and it take a while to enqueue all files
BroadcastReceiver receiver = new BroadcastReceiver()
{
@Override
public void onReceive( Context context, Intent intent )
{
DownloadManager manager = ( DownloadManager ) getSystemService( Context.DOWNLOAD_SERVICE );
String action = intent.getAction();
Log.d( "BroadcastReceiver", "action " + action );
if ( DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals( action ) )
{
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0 );
DownloadManager.Query query = new DownloadManager.Query();
Log.d( "BroadcastReceiver", "downloadId" + downloadId + " - " + manager.getUriForDownloadedFile( downloadId ) );
Cursor c = manager.query( query );
if ( c.moveToFirst() )
{
int columnIndex = c
.getColumnIndex( DownloadManager.COLUMN_STATUS );
if ( DownloadManager.STATUS_SUCCESSFUL == c
.getInt( columnIndex ) )
{
Log.d( "BroadcastReceiver", "complete" );
}
}
c.close();
}
}
};
registerReceiver( receiver , new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE ) );
After all files are enqueued, i start receiving complete callbacks, app is not totally frozen anymore but very laggy.
I read DownloadManager is executing in background so i don't get it. Thx for your help!
Upvotes: 0
Views: 1274
Reputation: 15775
There are multiple things at play here which are slowing your app down:
BroadcastReceiver
for each file and registering it. This causes new object creation and Cris process communication with the framework, both are expensive.Cursor
in the onReceive()
method. This is another potentially expensive processing call. The DownloadManager
is in another process, so the query is going cross process and cursor operations are often slow.Things to do to help:
BroadcastReceiver
to handle the response from DownloadManager
DownloadManager
to process completed files. The id
in the Intent
tells you which file completed, if you need more status information and to take an action, use some type of deferred processing like a thread.Upvotes: 1