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 8610957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:15:07+00:00 2026-06-12T04:15:07+00:00

I am writing a custom event and would like some help please. Most of

  • 0

I am writing a custom event and would like some help please. Most of what I am about to talk about is based on the help provided at Custom event listener on Android app

So here is my issue. I am writing an app that needs to download updated images from the web, store the images on the phone, then later display those images. Basically, I download any needed images during a splash screen. Then when the images are downloaded and stored, the splash screen clears and any necessary (newly downloaded) images are displayed on the screen. Here is the problem: the download process is done via an asynctask so the part where the images are loaded on to the screen can’t be done inside the asynctask. It has to be done on the main UI thread. I would like to create an event and a custom event listener for the main thread to listen for that basically tells the main UI thread that it is safe to start loading the downloaded images from memory.

According to the discussion from the link above, I came up with this so far… a download listener interace

public interface DataDownloadListener {
 void onDownloadStarted();
 void onDownloadFinished();
}

an event class…

 public class DataDownloadEvent {
      ArrayList<DataDownloadListener> listeners = new ArrayList<DataDownloadListener>();
      public void setOnDownload(DataDownloadListener listener){
           this.listeners.add(listener);
      }
 }

My problem is that I don’t understand where to put the last two steps in those instructions. I thought I would have to put the listener and event inside the class that actually initiates the downloads. But where? Here is my function that initiates the download and saves it to the device:

 public String download(String sourceLocation) {
    String filename = "";
    String path = "";
    try {
        File externalStorageDirectory = Environment
                .getExternalStorageDirectory();
        URL urlTmp = new URL(sourceLocation);
        filename = urlTmp.getFile()
                .substring(filename.lastIndexOf("/") + 1);
        path = externalStorageDirectory + PATH;

        // check if the path exists
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }

        filename = path + filename;
        f = new File(filename);

        //only perform the download if the file doesn't already exist
        if (!f.exists()) {

            Bitmap bitmap = BitmapFactory.decodeStream(urlTmp.openStream());
            FileOutputStream fileOutputStream = new FileOutputStream(
                    filename);
            if (bitmap != null) {
                bitmap.compress(getFormat(filename), 50, fileOutputStream);
                Log.d(TAG, "Saved image " + filename);
                return filename;
            }
        }
        else{
            Log.d(TAG, "Image already exists: " + filename + " Not re-downloading file.");
        }
    } catch (MalformedURLException e) {
        //bad url
    } catch (IOException e) {
        //save error
    }

    return null;
}

And the last step about registering the listener, where do I put that? The instructions say to put that somewhere during initialization. Does that mean in the onCreate method of my main activity? outside the class in the import section of the main activity? Never done a custom event before, so any help would be appreciated.

  • 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-12T04:15:08+00:00Added an answer on June 12, 2026 at 4:15 am

    According to the discussion from the link above, I came up with this so far… a download listener interace

    public interface DataDownloadListener {
    void onDownloadStarted();
    void onDownloadFinished();
    }

    an event class…

    public class DataDownloadEvent {
          ArrayList<DataDownloadListener> listeners = new ArrayList<DataDownloadListener>();
          public void setOnDownload(DataDownloadListener listener){
               this.listeners.add(listener);
          }
     }
    

    Ok…

    Now in your download procedure, at the start of the download, cycle all the elements on the listeners ArrayList and invoke the onDownloadStarted event to inform all your listeners that the download is just started (in this event i presume you’ll need to open the splashscreen).

    Always in your download procedure, at the and of the download, cycle all the elements on the listeners ArrayList and invoke the onDownloadFinished event to inform all your listeners that the download is finished (now close the splashscreen).

    How to cycle listeners on download completed

      foreach(DataDownloadListener downloadListener: listeners){
            downloadListener.onDownloadFinished();
      }
    

    How to cycle listeners on download started

      foreach(DataDownloadListener downloadListener: listeners){
            downloadListener.onDownloadStarted();
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some custom behavior for a ToolStripDropDown control. I'd also like to modify
I'm writing a custom ErrorHandler attribute for my MVC project. I would like to
I am writing a custom app for Rally and I would like to filter
I'm writing a jQuery plugin and I would like my code to take advantage
I'm writing a custom control and I'd like the control to switch from an
I'm writing my first Cocoa app and I would like to make a trackball
I am writing an iPhone application in Objective-C that utilizes some custom drawing in
I have a table into wich I'd like to mix some custom cells and
I'm having some difficulty using picture boxes as buttons. I'd like to have custom
I am writing a WPF application and I would like to use an Enum

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.