gpt4 book ai didi

android - 如何使用按钮打开/关闭加速度计?

转载 作者:行者123 更新时间:2023-11-29 21:50:32 27 4
gpt4 key购买 nike

我一直致力于记录加速度计的数据。尽管如此,我发现很难控制加速度计传感器的开/关。我试过有两个按钮,一个叫开始,另一个叫停止。我在 Activity 类的 onCreate() 回调中初始化了我的传感器对象。然后在按钮的 onClick() 回调中,我检查了 View 的 id,如果它是开始按钮,我为传感器注册监听,如果它停止,我注销它。这不按预期工作。在 onSensorChanged() 中,我只显示 x、y、z 加速度。无论我点击什么按钮,甚至没有点击任何按钮,传感器都会做它需要做的事情。好像是一旦sensor初始化了,就一直在读,没有办法停止。是否可以使用两个按钮控制传感器的开/关?

谢谢

更新:

package com.example.firstapp;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener,OnClickListener {
private float mLastX, mLastY, mLastZ;
private boolean mInitialized;

private SensorManager mSensorManager;

private Sensor mAccelerometer;

private final float NOISE = (float) 2.0;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.start);
Button stopButton = (Button) findViewById(R.id.start);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// can be safely ignored for this demo
}
@Override
public void onSensorChanged(SensorEvent event) {
TextView tvX= (TextView)findViewById(R.id.x_axis);
TextView tvY= (TextView)findViewById(R.id.y_axis);
TextView tvZ= (TextView)findViewById(R.id.z_axis);
ImageView iv = (ImageView)findViewById(R.id.image);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (!mInitialized) {
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText("0.0");
tvY.setText("0.0");
tvZ.setText("0.0");
mInitialized = true;
} else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if (deltaX < NOISE) deltaX = (float)0.0;
if (deltaY < NOISE) deltaY = (float)0.0;
if (deltaZ < NOISE) deltaZ = (float)0.0;
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText(Float.toString(deltaX));
tvY.setText(Float.toString(deltaY));
tvZ.setText(Float.toString(deltaZ));
iv.setVisibility(View.VISIBLE);
//if (deltaX > deltaY) {
//iv.setImageResource(R.drawable.horizontal);
//} else if (deltaY > deltaX) {
//iv.setImageResource(R.drawable.vertical);
//} else {
//iv.setVisibility(View.INVISIBLE);
//}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.start:
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
break;
case R.id.stop:
mSensorManager.unregisterListener(this,mAccelerometer);
break;
}

}
}

最佳答案

只需更新 unregisterListener。

  case R.id.stop:
// mSensorManager.unregisterListener(this,mAccelerometer);
mSensorManager.unregisterListener(this);
break;

关于android - 如何使用按钮打开/关闭加速度计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513864/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com