按下按钮获取加速度计值

时间:2013-03-27 06:17:38

标签: android android-sensors

我正在尝试一个示例项目,用于在点击按钮上存储android加速度计值,它正在工作,我得到了值,现在我想存储加速度计读数从按钮点击开始并结束按钮释放,我尝试过使用 OnTouchListener 但是我无法控制加速计,以前任何人都试过这个,任何一种答案都会对我有所帮助

layout.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
            mAccelerometer = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sensorMgr.registerListener((SensorEventListener) getApplicationContext(), mAccelerometer,SensorManager.SENSOR_DELAY_FASTEST);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            sensorMgr.unregisterListener(this);
        }
        return true;
    }
});

这是我用来控制加速度计的代码,但是我的应用程序强制关闭给出了logcat的错误

03-27 12:36:29.570: I/TextToSpeech.java(28968): initTts() successfully bound to service
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 1
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 2
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 3
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 4
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 5
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 6
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 7
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 0
03-27 12:36:30.570: W/dalvikvm(28968): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-27 12:36:30.578: E/AndroidRuntime(28968): FATAL EXCEPTION: main
03-27 12:36:30.578: E/AndroidRuntime(28968): java.lang.ClassCastException: android.app.Application
03-27 12:36:30.578: E/AndroidRuntime(28968):    at com.example.androidphonemotion.MainActivity$1.onTouch(MainActivity.java:59)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.view.View.dispatchTouchEvent(View.java:3934)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1733)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1151)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1717)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2215)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1886)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.os.Looper.loop(Looper.java:130)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at android.app.ActivityThread.main(ActivityThread.java:3689)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at java.lang.reflect.Method.invokeNative(Native Method)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at java.lang.reflect.Method.invoke(Method.java:507)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-27 12:36:30.578: E/AndroidRuntime(28968):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

我注册并取消注册按钮上的传感器监听器点击,可以通过为传感器设置onSensorChangedListener来执行存储值。

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

public void onSensorChanged(SensorEvent event) {
    //perform storing values of event
}

或者,您可以在onResume()中注册并在onPause()中取消注册。要开始/停止存储值,您可以在onTouch中设置一个标志(如果您想在按住按钮时进行记录)或onClick(从第一个按钮开始触摸并在第二个按钮时停止)。然后,您必须在onSensorChanged(SensorEvent event)中添加另一个if子句,以便只有在flag为true时才会存储信息。这样你就不会在注册听众方面遇到问题,但只要应用程序正在运行,传感器就会一直处于活动状态,因此电池消耗可能会更高。