我一直在创建这个Android应用程序,并且在此过程中学到了很多关于java和android编程的知识。但是,我只是无法弄清楚如何使用延时循环。我查看了其他堆栈帖子并看到了不同的方法,但无法成功实现它们。我将描述我想要循环的内容。
当我触摸Android应用程序的屏幕时,我希望应用程序说话(“开始测试”)。在接下来的20秒内,我希望应用程序每50毫秒调用一次angmag,这样我就可以得到角速度的大小,看看它是否高于某个阈值。此功能每50毫秒调用20秒,直到应用程序说“测试完成”然后完成测试。我将如何在Android应用程序中实现此延迟功能?我已经评论了我想要延迟代码的地方。
package com.example.shivamgandhi.gyrosafe;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Balance_Test_Activity extends VoiceControl implements View.OnClickListener, SensorEventListener, View.OnTouchListener {
int n = 2;
Button btnarray[] = new Button[n];
private SensorManager sManager;
private TextView textView79;
RelativeLayout RelativeLayout;
int count = 0;
double arg0, arg1, arg2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.balance_test);
RelativeLayout = (RelativeLayout)findViewById(R.id.RelativeLayout4);
btnarray[0] = (Button)findViewById(R.id.button10);
btnarray[1] = (Button)findViewById(R.id.button20);
sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
textView79 = (TextView)findViewById(R.id.textView79);
for(int i = 0; i <n; i++){
btnarray[i].setOnClickListener(this);
}
RelativeLayout.setOnTouchListener(this);
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible*/
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onStop()
{
//unregister the sensor listener
sManager.unregisterListener(this);
super.onStop();
}
@Override
public void onClick(View v){
if(v == findViewById(R.id.button10)){
Intent myIntent = new Intent(this, Results_Page_Activity.class);
startActivity(myIntent);
}
if(v == findViewById(R.id.button20)){
//implement
int gyro_results = gyrotest();
}
}
@Override
public boolean onTouch(View v, MotionEvent event){
if(v == RelativeLayout && count ==0 ){
gyrotest();
count = 1;
return true;
}
else{
return false;
}
}
public int gyrotest(){
int gyro_results = 0;
speakWords("Please place your hands on your hips and keep your feet under your shoulders. Hold your balance for the next 20 seconds");
Thread.sleep(5000); //delay 5 seconds;
int count = 0;
int angmag = 0;
while(count < 401){
if (Math.sqrt((arg0*arg0 + arg1*arg1 + arg2*arg2)) > 20){
angmag = 1;
}
else{
angmag = 0;
}
gyro_results = angmag + gyro_results;
count++;
Thread.sleep(50000);
}
return gyro_results;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
public void onSensorChanged(SensorEvent event)
{
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
arg0 = event.values[0];
arg1 = event.values[1];
arg2 = event.values[2];
//else it will output the Roll, Pitch and Yawn values
textView79.setText("Orientation X (Roll) :"+ Float.toString(event.values[2]) +"\n"+
"Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
"Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
}
}
我假设代码看起来像这样:
delay 5 seconds;
int count = 0;
while count < 401{
gyro_results = angmag + gyro_results;
count++;
delay 50 milliseconds;
}
return gyro_results;
我希望我能够弄清楚如何自己编写代码,但我尝试了大多数方法。感谢先进的任何帮助:)
编辑:使用Thread.sleep函数在修改后的代码中添加。但是,我仍然看到Thread.sleep存在一些问题。
答案 0 :(得分:0)
在Thread中使用sleep()方法进行延迟处理。
try
{
Thread.sleep(5000); //delay 5 seconds;
int count = 0;
while count < 401{
gyro_results = angmag + gyro_results;
count++;
Thread.sleep(50000); //delay 50 milliseconds
}
}catch(InterruptedException ie)
{
//Log message if required.
}