如何从单个 Intent Service 执行多个下载操作并更新每个操作的进度

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

我写了通过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);
    }
}

我的 AlarmManager BroadcastReceiver

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);

            }
        }
    }
}

简单地调用我的 Intent 服务

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

此代码运行良好,我正在接收单个下载操作的进度更新。

现在,我想使用这个 IntentService 执行多个下载操作,并更新每个操作的进度。您能否通过此代码检查是否可行或提供其他替代解决方案。

提前致谢。

使用Handler就可以得到结果。

请看here.