Question

I have a problem. My application do some beep, but when i close the app the beep continues, and to end it i must close the app from "task manager". This is the where is the beep sounds.

private void playAlertTone(final Context ctx){

        t = new Thread()
        {
            public void run()
            {
                player = MediaPlayer.create(ctx, R.raw.beep_1);
                while(true)
                {
                    if( appDegree != -1 )
                    {
                        if( getDistance() <= 5 )
                            player = MediaPlayer.create(ctx, R.raw.beep_4);
                        else if( getDistance() <= 50 )
                            player = MediaPlayer.create(ctx, R.raw.beep_3);
                        else if( getDistance() <= 120 )
                            player = MediaPlayer.create(ctx, R.raw.beep_2);
                        else
                            player = MediaPlayer.create(ctx, R.raw.beep_1);
                    }
                    else
                        player = MediaPlayer.create(ctx, R.raw.beep_1);
                    player.start();
                    try
                    {
                        Thread.sleep(player.getDuration());
                        player.release();
                    } catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();  
    }`

Yes I'm just switching from the app to the home screen.

It work with the some_public_flag into the while and with

`

 @Override
 public void onStop(){
   super.onStop();
   some_public_flag = false;
 }`
Was it helpful?

Solution 2

You need to exit the while(true) loop somehow. Set a global variable to true when you start this method and when you exit the app / or a specific screen set that variable to false.

If you do it from activity:

public void onDestroy() {
    super.onDestroy();
    if(isFinishing()) {
        // set the variable to false
    }
}

Your method would change to: private void playAlertTone(final Context ctx){

    t = new Thread()
    {
        public void run()
        {
            some_public_flag = true;
            player = MediaPlayer.create(ctx, R.raw.beep_1);
            while(some_public_flag)
            {
                if( appDegree != -1 )
                {
                    if( getDistance() <= 5 )
                        player = MediaPlayer.create(ctx, R.raw.beep_4);
                    else if( getDistance() <= 50 )
                        player = MediaPlayer.create(ctx, R.raw.beep_3);
                    else if( getDistance() <= 120 )
                        player = MediaPlayer.create(ctx, R.raw.beep_2);
                    else
                        player = MediaPlayer.create(ctx, R.raw.beep_1);
                }
                else
                    player = MediaPlayer.create(ctx, R.raw.beep_1);
                player.start();
                try
                {
                    Thread.sleep(player.getDuration());
                    player.release();
                } catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();  
}

OTHER TIPS

This happens because when you close the activity the thread continues to work. You should manage to interrupt the thread in the onStop activity method. Try to add this code:

@Override
public void onStop(){
   super.onStop();
   t.interrupt();
}

Anyway, when you say "i close the app" in Android can be ambiguous: are you exiting from the app or just switching from the app to the home screen? In the first case you have to call the function finish() in an activity (for ex.: inside an onClickListener or in the onClose method). If you are just switching to the home screen pressing the back button you are not closing the app but just moving your activity to the background in a pause mode which doesn't prevent threads to finish to run.

If you want to awake your thread when the user switch back to the app you can override the onStart method.

@Override
public void onStart(){
   super.onStart();
   t.run();
}

NB: the onDestroy method is called just when the app actually exits, if it is in the background it is just in a Stop mode. You can read more about the activity lifecycle here.

You must call .finish() to close the whole app because it will still run in background and beeping.. are you sure that you are finishing your app?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top