Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8721407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:13:00+00:00 2026-06-13T07:13:00+00:00

I’ve written a filecache that allows to load files from the web. Activities send

  • 0

I’ve written a filecache that allows to load files from the web. Activities send requests to this filecache and give the name and action of a BroadcastReceiver that get’s notified when the requested file has been downloaded.

This cache is working but it has a small drawback.

If there are lots of files in the download queue the activities are notified at once after processing all downloads. I would like to send the broadcast for each downloaded file.

Here’s the stripped down code. Currently a Handler gets fired after processing all the files in the Thread.run(). I would like to send the Broadcast from within the run. What would be the prefered way to do something (send a Broadcast) during Thread.run()?

Many thanks in advance.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:13:01+00:00Added an answer on June 13, 2026 at 7:13 am

    I would say sending an Intent with a “FILE_COMPLETED” action, and adding a String extra with the file’s name and have all your activities register a BroadcastReceiverwith that same action but compare whether or not the String extra within the Intentmatches the file that activity wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.