简体   繁体   English

无法根据 Android Studio 中的加速度计数据更新应用程序

[英]Can't get app to update based on accelerometer data in Android Studio

I am trying to clear the text labels/output when the phone is shaken by using accelerometer data.我正在尝试使用加速度计数据在手机震动时清除文本标签/输出。

This is what I have:这就是我所拥有的:

imports ....
public class Main_Activity extends AppCompatActivity implements TextToSpeech.OnInitListener, SensorEventListener{

    public void onAccuracyChanged(Sensor arg0, int arg1){

    }

    public void onSensorChanged(SensorEvent event){

        double ax = event.values[0];
        double ay = event.values[1];
        double az = event.values[2];
        double a = Math.sqrt(ax*ax + ay*ay + az*az);

        if(a > 20){
            ((EditText) findViewById(R.id.pBox)).setText("");
            ((EditText) findViewById(R.id.aBox)).setText("");
            ((EditText) findViewById(R.id.iBox)).setText("");
            ((TextView) findViewById(R.id.output)).setText("");
        }


    public void buttonClicked(View v){
        // getting values entered by user and showing output
    }
}

Everything works except when I shake my phone, the text doesn't clear.一切正常,除了当我晃动手机时,文本不清晰。

Please create these as global var请创建这些作为全局变量

private float acelVal,acelLast,shake;

Please use this login to get shake events in your onSensorChanged()请使用此登录名在您的onSensorChanged()获取震动事件

float x =event.values[0];
float y =event.values[1];
float z =event.values[2];
acelLast=acelVal;
acelVal=(float) Math.sqrt((double) (x*x)+(y*y)+(z*z));
float delta= acelVal-acelLast;
shake =shake*0.9f+delta;


if(shake>20){
    ((EditText) findViewById(R.id.pBox)).setText("");
    ((EditText) findViewById(R.id.aBox)).setText("");
    ((EditText) findViewById(R.id.iBox)).setText("");
    ((TextView) findViewById(R.id.output)).setText("");
}

This is the original answer这是原答案

Hope this will help!希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM