如何从正在运行的线程中发送广播

时间:2012-10-11 11:26:44

标签: java android multithreading handler

我写了一个文件缓存,允许从网上加载文件。活动向此文件缓存发送请求,并提供BroadcastReceiver的名称和操作,以便在下载所请求的文件时收到通知。

此缓存正在运行,但它有一个小缺点。

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

这是精简代码。目前,在处理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);
    }
}

1 个答案:

答案 0 :(得分:0)

我会说发送一个带有“FILE_COMPLETED”操作的Intent,并添加一个带有文件名称的字符串,并让所有活动注册BroadcastReceiver同样的操作,但是要比较是否Intent中的字符串extra与活动所需的文件匹配。