3

I am in the process of making a simple practice Morse code application.

I am trying to make an Image View flash between black and white in the SOS sequence based on Morse. When researching how to achieve this i realized i would have to do this on a separate thread in order to not block the UI thread.

The problem is that i am currently updating the Image View from outside of the UI thread which is stated to be a bad idea. At the moment i am trying to pass the Image View to the worker thread class that contains the logic for the screen flash.

I am new to multi-threading and i am pretty sure i am doing this completely backwards/incorrectly. Does anyone have an advice or ideas as of the best method to go about this?

  new Thread(new Runnable() {
            public void run() {

                sf = new ScreenFlash("...---...", imgV);
                sf.flashScreen();
            }
        }).start();


public ScreenFlash(String message, ImageView imgV){

      this.message = message.replaceAll(".(?!$)", "$0 ");

      this.imgV = imgV;

     }


  public void flashScreen() {

    int offIntervalTime = 50;

    char[] cArray = message.toCharArray();

    for (int i = 0; i < cArray.length; i++) {
        if (cArray[i] == '.') {
            imgV.setBackgroundColor(Color.WHITE);

            try {
                Thread.sleep(50);
            }catch(Exception e)
            {

            }

            imgV.setBackgroundColor(Color.BLACK);


            try {
                Thread.sleep(offIntervalTime);
            }catch(Exception e)
            {

            }

        } else if(cArray[i] == ' ')
        {
            try{

                Thread.sleep(100);
            }catch(Exception e)
            {

            }
        }
        else {

            try{
               imgV.setBackgroundColor(Color.WHITE);
                Thread.sleep(dash);
            }catch(Exception e)
            {

            }
            try{
                imgV.setBackgroundColor(Color.BLACK);
                Thread.sleep(offIntervalTime);
            }catch(Exception e)
            {

            }

        }
    }
Perfect_Comment
  • 165
  • 1
  • 2
  • 15
  • Take a look at `ASyncTask`. Android does not work very well with `Thread`s. `ASyncTask`s also allow you to update `View`s by design if you override `onProgressUpdate`. https://developer.android.com/reference/android/os/AsyncTask.html – GiantTree Jan 08 '16 at 15:06
  • Is it not easier to simply animate your imageview from black to white (visible,invisible) ? See : http://stackoverflow.com/questions/4852281/android-how-can-i-make-a-button-flash/4852468#4852468 – grunk Jan 08 '16 at 15:12
  • I have looked into the ASyncTask and i am still attempting to figure out exactly how it works - i require 3-4 threads running at simultaneously and from what i gather this is possible using ASyncTask it is just new territory to me but i will research it. In regards animating the Image view - so you mean using this method to replace the for loop? – Perfect_Comment Jan 08 '16 at 15:51
  • After looking into ASyncTask i see that i can access the UI but the problem is that i need to access it multiple times - from what i gather i can only access the UI once. Maybe i am incorrect? – Perfect_Comment Jan 09 '16 at 23:41

1 Answers1

0

Take a look at the Handler class documentation and how to communicate with the UI thread from a background thread: http://developer.android.com/training/multiple-threads/communicate-ui.html

You can update your imageView in onHandleMessage()

    @Override 
    public void handleMessage(Message inputMessage) {
        imageView.setBackgroundColor(android.R.color.white)
        ... 
    }
wnieves19
  • 550
  • 4
  • 16