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();
}