0

我写了一个文件缓存,允许从网络加载文件。活动向此文件缓存发送请求,并提供广播接收器的名称和操作,当请求的文件已下载时,该接收器会收到通知。

这个缓存可以工作,但它有一个小缺点。

如果下载队列中有很多文件,则在处理完所有下载后立即通知活动。我想为每个下载的文件发送广播。

这是精简的代码。当前,在处理完 Thread.run() 中的所有文件后会触发 Handler。我想从运行中发送广播。在 Thread.run() 期间做某事(发送广播)的首选方式是什么?

提前谢谢了。

public abstract class MyFileCache {

    private static class CacheElement {
        private File file;
    }

    private static class QueueElement {
        private long action;
        private String filename = "";
        private long id;
        private String receiver = "";
    }

    private static class ProcessedElement {
        private long action;
        private File file;
        private long id;
        private String receiver = "";
    }

    private Map<String, CacheElement> cache = new ConcurrentHashMap<String, CacheElement>();
    private Context context;
    private Map<String, ProcessedElement> processed = new ConcurrentHashMap<String, ProcessedElement>();
    private Map<String, QueueElement> queue = new ConcurrentHashMap<String, QueueElement>();

    public MyFileCache(Context context) {
        this.context = context;
    }

    private void doThread() {
        final Handler handler = new Handler() {

            @Override
            public void handleMessage(Message message) {

                try {
                    for (Map.Entry<String, ProcessedElement> entry : processed.entrySet()) {
                        // Currently: Processing all fetched files at once
                        // Send for all entries a broadcast to the requesting activities
                        ProcessedElement processedElement = entry.getValue();
                        if (processedElement != null && processedElement.receiver != null) {
                            processSendBroadcast(processedElement.receiver,
                                    processedElement.action, processedElement.id);
                        }

                        deleteFromProcessed(entry.getKey());
                    }
                } catch (NullPointerException exception) {
                }
            }
        };

        new Thread() {

            @Override
            public void run() {
                for (Map.Entry<String, QueueElement> entry : queue.entrySet()) {
                    QueueElement queueElement = entry.getValue();
                    if (queueElement != null) {
                        File file = fetch(entry.getKey(), queueElement.id, queueElement.filename,
                                queueElement.receiver, queueElement.action);
                        if (file != null) {
                            // Wish: Sending a broadcast to the requesting activity for each fetched file
                        }
                    }
                }

                handler.sendEmptyMessage(0);
            }
        }.start();
    }

    private void deleteFromProcessed(String url) {
        if (processed.containsKey(url)) {
            ProcessedElement processedElement = processed.get(url);
            if (processedElement != null) {
                processed.remove(url);
            }
        }
    }

    // Send broadcast
    private void processSendBroadcast(String receiver, long action, long id) {
        Intent intent = new Intent();
        intent.putExtra("ACTION", action);
        intent.putExtra("ID", id);
        intent.setAction(receiver);
        context.sendBroadcast(intent);
    }
}
4

1 回答 1

0

我会说发送一个Intent带有“FILE_COMPLETED”操作的字符串,并添加一个带有文件名的额外字符串,并让您的所有活动都BroadcastReceiver使用相同的操作注册一个,但比较文件中的额外字符串是否Intent与活动想要的文件匹配。

于 2012-10-11T11:31:01.803 回答