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

时间:2015-11-05 10:09:53

标签: android alarmmanager intentservice

我已经编写了通过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执行多个下载操作,并对每个操作进行更新。您能否通过此代码检查是否可行或提供其他替代解决方案。

提前致谢。

1 个答案:

答案 0 :(得分:0)

使用Handler可以达到效果。

请参阅here.