1

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

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

但不幸的是,它不适合我的情况,我遇到了 logcat 异常,比如'cant instantiate the default constructor of IntentService'

我真的被困在这种情况下。如果有人遇到过这种情况,请给我一些建议/代码片段。

这是我当前的示例代码: http: //pastebin.com/HCWFPtbb

4

1 回答 1

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

}

于 2012-09-04T14:12:20.680 回答