在所有活动中使用服务更新ProgressBar

时间:2012-09-04 13:48:52

标签: android multithreading service progress-bar intentservice

我有一个应用程序,根据要求,从后台线程处理下载图像。根据需要,我想在所有活动中显示水平进度条,同时下载照片。这也是负责整个Json解析我的应用程序的类。 为此,我实现了一个扩展Service并调用其onCreate方法的类。但我仍然不清楚如何更新当前的进度条,如果用户在其他屏幕上切换,进度条应相应更新。 以前我尝试使用IntentService类,但它需要

MyService (String name){
  super(name);
}

但不幸的是,它不适合我的情况,我不得不像'cant instantiate the default constructor of IntentService'

那样得到logcat异常

我真的陷入了这种境地。如果有任何人面临这种​​情况,请建议我一些方法/代码片段。

以下是我目前的示例代码:http://pastebin.com/HCWFPtbb

1 个答案:

答案 0 :(得分:0)

public final class DownloadThread extends Thread
    {

        private static final String       TAG           = DownloadThread.class.getSimpleName();

        private Handler                   handler;

        private int                       totalQueued;

        private int                       totalCompleted;

        private final DownloadThreadListener listener;

        private boolean                   isRunning   = false;

        protected boolean                   isStop;

        private TaskInitiator               mtaskInitiator = null;

        /**
         * @return the isRunning
         */
        public boolean isRunning()
            {
                return isRunning;
            }

        public DownloadThread(DownloadThreadListener listener)
            {
                this.listener = listener;
                isStop = false;
            }

        @Override
        public void run()
            {
                try
                    {

                        Looper.prepare();

                        mtaskInitiator = new TaskInitiator();
                        handler = new Handler();
                        isRunning = true;

                        Looper.loop();

                    }
                catch (Throwable t)
                    {
                        Log.e(TAG, "DownloadThread halted due to an error", t);
                    }
            }

        public synchronized void requestStop()
            {

                isStop = true;

            }

        public synchronized void enqueueDownload(DownloadTask task)
            {

                mtaskInitiator = new TaskInitiator();
                mtaskInitiator.setTask(task);
                handler.post(mtaskInitiator);
                totalQueued++;

            }

        public synchronized void removeTasks()
            {

                if (mtaskInitiator != null)
                    {

                        handler.removeCallbacks(mtaskInitiator);
                        totalQueued = 0;
                        totalCompleted = 0;
                    }
            }

        class TaskInitiator implements Runnable
            {

                private DownloadTask task = null;

                public DownloadTask getTask()
                    {
                        return task;
                    }

                public void setTask(DownloadTask task)
                    {
                        this.task = task;
                    }

                public TaskInitiator()
                    {
                        // TODO Auto-generated constructor stub
                    }

                @Override
                public void run()
                    {
                        try
                            {
                                task.run();

                            }
                        finally
                            {

                                if (isStop)
                                    {
                                        Looper.myLooper().quit();
                                    }
                                synchronized (DownloadThread.this)
                                    {
                                        signalUpdate(task.getCurrentBitmap(), task.getCurrentFeedUrl());
                                        task.setCurrentBitmap(null);
                                        totalCompleted++;

                                    }

                            }

                    }

            }

        public synchronized int getTotalQueued()
            {
                return totalQueued;
            }

        public synchronized int getTotalCompleted()
            {
                return totalCompleted;
            }

        public void signalUpdate(Bitmap bmap, String bmapUrl)
            {

                if (listener != null)
                    {
                        listener.handleDownloadThreadUpdate(bmap, bmapUrl);
                    }
            }

    }

让你的Activity组件为Thread类的回调实现以下接口。

public interface DownloadThreadListener {

void handleDownloadThreadUpdate(Bitmap bmap,String bmapUrl);

Map<String, Bitmap> giveImages_Map();

ConcurrentHashMap<String, SoftReference<Bitmap>> giveSoftImages_Map();

}