we’re trying to build an application that records the decibel level every seconds or so. The problem is that we need to have an infinite loop in the run() method that polls the decibel level, but when we add the infinite loop, it hangs. Could anyone point us to the right direction? Thanks.
Here’s the code:
package tpsip.tpsip;
import android.app.Activity;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.TextView;
public class tpsip extends Activity implements Runnable{
private TextView decibelMeter;
private TextView timeRemaining;
private Button button;
private int safetyTime;
ExposureCalculator calculator = new ExposureCalculator();
Handler handler = new Handler();
Thread thread = new Thread();
LogicLayer logicLayer = new LogicLayer();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
this.safetyTime = 28800; //8 hours of safe time when the decibel level is between 90-92, levels below this are not considered dangerous to hearing
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.decibelMeter = (TextView)findViewById(R.id.textView2);
this.decibelMeter.setText("0 dB");
this.timeRemaining = (TextView)findViewById(R.id.textView3);
this.timeRemaining.setText("Safety time remaining " + safetyTime + " seconds");
this.button = (Button) findViewById(R.id.button1);
button.setBackgroundColor(Color.GREEN);
button.setText("Ear protection not needed");
handler.post(thread);
this.run();
}
@Override
public void run() {
int a = 10;
while(true) {
this.decibelMeter.setText("0safsafsafa dB");
if (a == Math.random() * 100) break;
}
}
}
Run the opertaion on a different thread and change the UI when needed from there using handler or
runOnUIThread(). currently the method is being called as a regullar method and runs in infinite loop which doesn’t let your UI be updated.