我是否需要为传感器创建单独的按钮方法?

时间:2014-02-14 13:29:59

标签: android android-button android-sensors

我目前为我的应用程序创建了一个加速度计,我想创建两个按钮,我可以启动和停止加速度计来收集数据。目前,加速度计不断运行。我想知道我是否必须为这些按钮创建一个单独的onclick方法?正如我现在所做的那样,我的手机/日食不起作用。任何帮助将不胜感激。我的按钮代码在onResume()方法和onPause()方法,是正确的吗?

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.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class movement extends Activity implements SensorEventListener {

    private float mLastX, mLastY, mLastZ;
    private boolean mInitialized;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private final float NOISE = (float) 2.0;
    Button startButton, stopButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.movement);
        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();
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              mSensorManager.registerListener(movement.this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

            }
        });


    }

    protected void onPause() {
        super.onPause();
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               mSensorManager.unregisterListener(movement.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);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

不,这不太正确。您应该在onCreate()方法中添加Clicklisteners 你的活动,因为只有当活动再次被带到前面时才会调用onResume(),而只有当它移动到后台时才调用onPause()。

有关活动生命周期的更多信息,请参阅google文档的这一部分: http://developer.android.com/guide/components/activities.html

这是我所指的图片:
http://developer.android.com/images/activity_lifecycle.png

此外,您可以使用布局XML文件中的onClick标记将方法调用添加到按钮。您通常不必在预定义按钮上实现自己的侦听。

答案 1 :(得分:0)

嗯,你需要一个动作来阻止传感器,所以stopButton需要他自己的听众,而且很容易

startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              mSensorManager.registerListener(movement.this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

            }
        });
stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              // your code
            }
        });

尊重在onResume中创建侦听器,最好在onCreate中创建,因为侦听器将只在activity lifecycle中创建一次,除非你创建了onResume,他们会创造更多一次。想象一下,您创建活动(调用onResume),导航到另一个活动,然后返回活动(再次调用onResume