【问题标题】:Android delay loop to collect data from the gyroscopeAndroid 延迟循环从陀螺仪收集数据
【发布时间】:2016-03-08 13:04:50
【问题描述】:

我一直在创建这个 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 存在一些问题。

【问题讨论】:

    标签: android delay


    【解决方案1】:

    在线程中使用 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.
    }
    

    【讨论】:

    • 我在Thread.sleep中添加的时候,有红条。有什么我必须导入的吗?
    • 导入android.os.Bundle;导入android.os.Handler;
    • 不幸的是,这并没有解决问题。我将在上面粘贴更新后的代码。
    • 是的。线程.sleep(5000);函数下面有一个红色条。它说“未处理的异常:java.lang.InterruptedException”
    • 我没有看到任何错误。如果这工作正常,我会检查你的答案。谢谢!
    猜你喜欢
    • 2022-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多