如何从 onResume 访问陀螺仪?
How can I access the gyroscope from onResume?
我有一些代码旨在计算用户转得太快的次数。我实现了一个陀螺仪,并在 Resume 中使用了一些代码来收集数据以及一些语音输出。但是,我在使陀螺仪在 onResume 中正常工作时遇到问题。其一,语音输出无法正常工作,并且出现故障。接下来,我仍然没有得到陀螺仪的值。
package com.example.shivamgandhi.gyrosafe;
import android.content.Context;
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.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 mSensorManager;
private Sensor default_gyro;
private TextView textView79;
RelativeLayout RelativeLayout;
int count1 = 0;
float axisX, axisY, axisZ;
double omegaMagnitude;
EditText ed29;
private static final float NS2S = 1.0f / 1000000000.0f;
private final float[] deltaRotationVector = new float[4];
int touch = 0;
int count2 = 0;
int time1 = 0;
int time2 = 0;
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);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
default_gyro = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
textView79 = (TextView)findViewById(R.id.textView79);
ed29 = (EditText)findViewById(R.id.editText29);
for(int i = 0; i <n; i++){
btnarray[i].setOnClickListener(this);
}
RelativeLayout.setOnTouchListener(this);
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
if (touch == 1){
try{
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(2000);
while(time1 < 334){
mSensorManager.registerListener(this, default_gyro, SensorManager.SENSOR_DELAY_UI);
if(omegaMagnitude > 2){
count1 ++;
time1 ++;
}
else{
time1 ++;
}
}
Thread.sleep(2000);
speakWords("Place your hands by your side. Tap to continue.");
Thread.sleep(1000);
touch = 10;
}
catch(InterruptedException e){
e.printStackTrace();
}
}
if (touch == 2){
speakWords("Please place your hands on your hips and stand on your nondominant foot for the next 20 seconds");
while(time2 < 334){
mSensorManager.registerListener(this, default_gyro, SensorManager.SENSOR_DELAY_UI);
if(omegaMagnitude > 2){
count2 ++;
time1 ++;
}
else{
time1 ++;
}
}
speakWords("You may resume normal stance now");
touch = 20;
}
}
@Override
protected void onStop()
{
//unregister the sensor listener
mSensorManager.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);
}
}
@Override
public boolean onTouch(View v, MotionEvent event){
if(v == RelativeLayout){
if(touch == 0){
touch = 1;
onResume();
return true;
}
else if (touch == 10){
touch = 2;
onResume();
return true;
}
return true;
}
else{
return false;
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
public void onSensorChanged(SensorEvent event) {
// This timestep's delta rotation to be multiplied by the current rotation
// after computing it from the gyro sample data.
// Axis of the rotation sample, not normalized yet.
float axisX = event.values[0];
float axisY = event.values[1];
float axisZ = event.values[2];
// Calculate the angular speed of the sample
double omegaMagnitude = Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
float[] deltaRotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
textView79.setText("Gyro X (Roll) :" + Float.toString(event.values[2]) + "\n" +
"Gyro Y (Pitch) :" + Float.toString(event.values[1]) + "\n" +
"Gyro Z (Yaw) :" + Float.toString(event.values[0]));
}
}
如何修复此代码,以便我可以实际从陀螺仪中检索值?提前致谢:)
好的,这里有几个问题。
1)永远不要在 onResume 或 UI 线程的任何其他地方休眠。你不能那样坚持下去。最好的情况下这样做会让你看起来像是崩溃了,最坏的情况是它实际上会因为看门狗而崩溃。它几乎永远不会做你想做的事。
2)你从来没有真正设置 imegaMagnitude,因为你在 onSensorChanged
中重新定义了它
3) 无论如何您都不会看到正确的值,因为在调用 onSensorChanged 之前不会设置它。由于它仅在 UI 线程上调用,因此只有在 onResume 完成后才会调用它(请参阅 1)。所以你的代码无论如何都不能工作。
4)不能自己调用onResume。只允许框架调用生命周期函数。自己这样做会严重把事情搞砸。
你的做法完全不可行。重写您的逻辑以使用基于事件的处理和状态机模式。此代码不可挽救。
我有一些代码旨在计算用户转得太快的次数。我实现了一个陀螺仪,并在 Resume 中使用了一些代码来收集数据以及一些语音输出。但是,我在使陀螺仪在 onResume 中正常工作时遇到问题。其一,语音输出无法正常工作,并且出现故障。接下来,我仍然没有得到陀螺仪的值。
package com.example.shivamgandhi.gyrosafe;
import android.content.Context;
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.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 mSensorManager;
private Sensor default_gyro;
private TextView textView79;
RelativeLayout RelativeLayout;
int count1 = 0;
float axisX, axisY, axisZ;
double omegaMagnitude;
EditText ed29;
private static final float NS2S = 1.0f / 1000000000.0f;
private final float[] deltaRotationVector = new float[4];
int touch = 0;
int count2 = 0;
int time1 = 0;
int time2 = 0;
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);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
default_gyro = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
textView79 = (TextView)findViewById(R.id.textView79);
ed29 = (EditText)findViewById(R.id.editText29);
for(int i = 0; i <n; i++){
btnarray[i].setOnClickListener(this);
}
RelativeLayout.setOnTouchListener(this);
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
if (touch == 1){
try{
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(2000);
while(time1 < 334){
mSensorManager.registerListener(this, default_gyro, SensorManager.SENSOR_DELAY_UI);
if(omegaMagnitude > 2){
count1 ++;
time1 ++;
}
else{
time1 ++;
}
}
Thread.sleep(2000);
speakWords("Place your hands by your side. Tap to continue.");
Thread.sleep(1000);
touch = 10;
}
catch(InterruptedException e){
e.printStackTrace();
}
}
if (touch == 2){
speakWords("Please place your hands on your hips and stand on your nondominant foot for the next 20 seconds");
while(time2 < 334){
mSensorManager.registerListener(this, default_gyro, SensorManager.SENSOR_DELAY_UI);
if(omegaMagnitude > 2){
count2 ++;
time1 ++;
}
else{
time1 ++;
}
}
speakWords("You may resume normal stance now");
touch = 20;
}
}
@Override
protected void onStop()
{
//unregister the sensor listener
mSensorManager.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);
}
}
@Override
public boolean onTouch(View v, MotionEvent event){
if(v == RelativeLayout){
if(touch == 0){
touch = 1;
onResume();
return true;
}
else if (touch == 10){
touch = 2;
onResume();
return true;
}
return true;
}
else{
return false;
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
public void onSensorChanged(SensorEvent event) {
// This timestep's delta rotation to be multiplied by the current rotation
// after computing it from the gyro sample data.
// Axis of the rotation sample, not normalized yet.
float axisX = event.values[0];
float axisY = event.values[1];
float axisZ = event.values[2];
// Calculate the angular speed of the sample
double omegaMagnitude = Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
float[] deltaRotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
textView79.setText("Gyro X (Roll) :" + Float.toString(event.values[2]) + "\n" +
"Gyro Y (Pitch) :" + Float.toString(event.values[1]) + "\n" +
"Gyro Z (Yaw) :" + Float.toString(event.values[0]));
}
}
如何修复此代码,以便我可以实际从陀螺仪中检索值?提前致谢:)
好的,这里有几个问题。
1)永远不要在 onResume 或 UI 线程的任何其他地方休眠。你不能那样坚持下去。最好的情况下这样做会让你看起来像是崩溃了,最坏的情况是它实际上会因为看门狗而崩溃。它几乎永远不会做你想做的事。
2)你从来没有真正设置 imegaMagnitude,因为你在 onSensorChanged
中重新定义了它3) 无论如何您都不会看到正确的值,因为在调用 onSensorChanged 之前不会设置它。由于它仅在 UI 线程上调用,因此只有在 onResume 完成后才会调用它(请参阅 1)。所以你的代码无论如何都不能工作。
4)不能自己调用onResume。只允许框架调用生命周期函数。自己这样做会严重把事情搞砸。
你的做法完全不可行。重写您的逻辑以使用基于事件的处理和状态机模式。此代码不可挽救。