Android random time delayed user abortable infinite loop

by Ronoli » Wed, 22 Dec 2010 06:47:42 GMT

Sponsored Links
 Hello, can anyone show me the proper way to resolve my problem?,
I'm trying to to play a sound repeatedly but I need it to play with a
random delay.
When I grab a random number and start looping my sound, sure it plays
some random time later and continues to do so. But I want to use a new
random number for the delay between each loop.

Pseudo code:
1) Grab random number. (java.util.random).
2) Change that random number to represent seconds.
3) Pause or wait for that random amount of seconds.
4) Play sound from soundpool. (just mimic this as my soundpool is
working fine)
5) Grab new random number and repeat steps 3 through 5 and repeat this
process until the user cancels with a button.
6) Enable the user to cancel this loop at any time.

I would really appreciate a little help if someone can find the
Thank you in advance,


Re: Android random time delayed user abortable infinite loop

by TreKing » Wed, 22 Dec 2010 12:53:32 GMT


I do think I understand what your actual problem is. What, specifically, are
you having trouble with given your pseudo-code description?

TreKing <> ; - Chicago
transit tracking app for Android-powered devices


Sponsored Links

Re: Android random time delayed user abortable infinite loop

by Ron Richmond » Tue, 28 Dec 2010 01:51:09 GMT

 Hello and thanks for the reply,
It's really the user abortable loop that I'm having trouble with now.
Since first posting my problem, I have been researching code snippets and
have zeroed in on this one:

import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.widget.TextView;

public class DelayTimer extends Activity {

    TextView mTextField;
    long elapsed;
    final static long INTERVAL=1000;
    final static long TIMEOUT=5000;

    public void onCreate(Bundle savedInstanceState) {


        TimerTask task=new TimerTask(){
            public void run() {
                elapsed += INTERVAL;
                if(elapsed >= TIMEOUT){
                //if(some other conditions)
                //   this.cancel();
                displayText("seconds elapsed: " + elapsed / 1000);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);

    private void displayText(final String text){
        this.runOnUiThread(new Runnable(){
            public void run() {

I think I should be able to replace the static long INTERVAL with a call to
in a way that would work for me. However, I can't even get this example to
finish/abort/end properly.
After running this example, I have to go to "Manage Apps" and click "Force
Close" to kill it.
I'm not really sure if this is example is the best way to handle it either
but it is where I seem to be
zeroing in on. Any suggestions?
Thank you sooo much for your help,


Re: Android random time delayed user abortable infinite loop

by Bret Foreman » Tue, 28 Dec 2010 02:02:53 GMT

 Well first, you need a call to finish() somewhere in order to exit the


Re: Re: Android random time delayed user abortable infinite loop

by Ron Richmond » Tue, 28 Dec 2010 02:09:50 GMT

 Thank You,
I will be working on my problem today as I was pulled away from it for a few
days (Holidays!)
Thanks again, I really appreciate any and all help!!


Other Threads

1. Disable all other Layout Views while playing Animation within ImageView

I have a problem that i have a TableLayout. Where i have about 30
animated ImageViews.
Each ImageView is clickable. Animations are oneShot.

public boolean onClick(View v) {
                AnimationDrawable animation = (AnimationDrawable)


                        return true;
                 return false;

Problem is i want to make all other ImageViews disabled, while one
clicked is being played.
For now if i click on all of them very fast they will all start
playing same time.

Is there any nice way to disabling all other ImageViews besides that i
can take all of Views and setEnabled(false) like this:

ImageView imgView1 = (ImageView)findViewById(;
ImageView imgView2 = (ImageView)findViewById(;
ImageView imgView3 = (ImageView)findViewById(;
ImageView imgView4 = (ImageView)findViewById(;
ImageView imgView5 = (ImageView)findViewById(;

Because after it is played i will have to enable them all back...

And other question, is there is no other nice way to get a callback
from AnimationDraw, when it is finished playing?? Cuz this is really


2. Urgent Opening for Android Developers -- Malvern, PA & Bay Area, CA

Hello All,
We have few very urgent requirements for Android Developers at these

Malvern, PA & Bay Area, CA.

Please forward resume along with contact details to: [at]

Thank you,
AIT Resource Group, Inc.


3. Android Market Licensing: Now Available!

4. GingerBread Src Code

5. appwidgets in other apps

6. OS Access WiLink hardware ANT+

7. webview loading issue