Search code examples
androidalarmmanagerintentservice

How to perform multiple download operations from a single Intent Service with progress update of each operation


I have written code for downloading images through Intent Service

public class UIIntentService extends IntentService {

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     */
    public UIIntentService() {
        super("UIIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        downLoadManager();
    }

    private void downLoadManager() {

        // Starting download manager
        final DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse("http://some_url/xyz.jpg"));
        MainActivity.enqueue = manager.enqueue(request);

        scheduleAlarm(MainActivity.enqueue);
}

    public void scheduleAlarm(long id) {
        Intent intent = new Intent(getBaseContext(), com.example.com.downloaddemo.DisplayInoker.class);
        intent.putExtra("ID", ""+id);

        final PendingIntent pIntent = PendingIntent.getBroadcast(getBaseContext(), com.example.com.downloaddemo.DisplayInoker.REQUEST_CODE,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        long firstMillis = System.currentTimeMillis(); // alarm is set right away
        AlarmManager alarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);

        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
                3000, pIntent);
    }
}

My BroadcastReceiver for AlarmManager

public class DisplayInoker extends BroadcastReceiver{

    public static final int REQUEST_CODE = 12345;
    public static final String ACTION = "DISPLAYRESULT";
    private long enqueue;



    // Triggered by the Alarm periodically 
    @Override
    public void onReceive(Context context, Intent intent) {

        EventBus bus = EventBus.getDefault();

        if(intent.getExtras().getString("ID") != null) {
            enqueue = Long.parseLong("" + intent.getExtras().getString("ID"));

            Log.d("Innnnnnnnnnnnnnnnnnnnnn", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

            final DownloadManager manager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
            DownloadManager.Query q = new DownloadManager.Query();
            q.setFilterById(enqueue);

            Cursor cursor = manager.query(q);
            cursor.moveToFirst();

            double bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
            double bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
            double percentComplete = 0;

            // checking whether file downloaded or not
            boolean downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
                    == DownloadManager.STATUS_SUCCESSFUL ? true : false;

            cursor.close();

            boolean stopDisplaying = false;

            if (!downloaded) {

                try {

                    percentComplete = (bytes_downloaded / bytes_total) * 100;
                    bus.post("" + (int) percentComplete);
                } catch (Exception e) {
                    percentComplete = 0;
                    e.printStackTrace();
                }
            } else {
                Log.d("completed", "////////////////////// end ///////////////////////////" + bytes_total + "  " + bytes_downloaded);
                percentComplete = (bytes_downloaded / bytes_total) * 100;

                bus.post("" + (int) percentComplete);

            }
        }
    }
}

Calling my Intent service simply

Intent intentservice = new Intent(MainActivity.this, UIIntentService.class);
                startService(intentservice);

This code is working fine and I am receiving progress updates of single download operation.

Now, I want to use this IntentService to perform multiple download operations with progress update of each operation. can you please check whether is it possible through this code or provide some other alternative solution.

Thanks in advance.


Solution

  • Using Handler you can achieve the result.

    Please see here.