Search code examples
android-studioimagebuttonthread-sleep

onClick event with delay


I've got an ImageButton which is declared in my layout xml. I've told it when it is clicked, to call a certain method, playSound.

Inside of playSound, the phone plays a sound which lasts for a certain amount of time, 5277 ms to be precise. I want playSound to be able to change the ImageResource of the ImageButton for a certain amount of time (5400 ms), and then change it back.

Here's my code:

ImageButton dBellButt;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_main);
    dBellButt.setImageResource(R.drawable.doorbell2);

public void playSound(View view) {
    dBellButt.setImageResource(R.drawable.doorbell);
    dBell.start();
    if (vibeOn) {
        Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibe.vibrate(500);
    } else {
        // No vibration
    }
    try {
        Thread.sleep(5400);
    } catch (InterruptedException e) {
    }
    dBellButt.setImageResource(R.drawable.doorbell2);
}

doorbell2 is an image of a lit doorbell. doorbell is an image of an unlit doorbell.

I can switch the position of the two images and it'll work just fine, ie: if I have the doorbell created lit, when the button is pressed, it'll stay lit until 5400 ms, after which the image changes. If I create the application with the doorbell 'lit,' I cannot change it at all.

Hopefully that made sense.

In short, I cannot get the ImageButton to change ImageResource. The setImageResource after the Thread.sleep(); works fine, but the one before it does not.

How come this isn't working?

Thanks!

Nathan

EDIT:

Figured I would attach my xml just in case.

<ImageButton
    android:id="@+id/dBellButton"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:scaleType="fitCenter"
    android:padding="0dp"
    android:background="@null"
    android:onClick="playSound"
    android:contentDescription="@string/ImgDesc" />

Cheers


Solution

  • I found a solution. I ended up using the timer class.

    Here's my code:

    public void playSound(View view) {
        dBell.start();
        if (vibeOn) {
            Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibe.vibrate(500);
        } else {
            // No vibration
        }
        Timer timer = new Timer();
        TimerTask picSwitch1 = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        dBellButt.setImageResource(R.drawable.doorbell);
                    }
                });
            }
        };
        TimerTask picSwitch2 = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        dBellButt.setImageResource(R.drawable.doorbell2);
                    }
                });
            }
        };
        timer.schedule(picSwitch1, 10);
        timer.schedule(picSwitch2, 5400);
    }
    

    Using this allows the picture to switch after the delayed time.

    The problem I was having with the sleep function was it would cause the application to freeze up (duh...). I couldn't use any of the other given interface. This solution fixes that problem. I'll attach the links I used to solve this.

    http://developer.android.com/reference/java/util/Timer.html

    http://blog.redkitmedia.com/timer-android-example/

    Android timer/timertask causing my app to crash?