-1

我正在尝试一个示例项目,用于在单击按钮时存储 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)
4

1 回答 1

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 (从第一个按钮触摸开始并在第二个按钮触摸停止)中设置一个标志。然后,您必须在您的中添加另一个 if 子句,onSensorChanged(SensorEvent event)这样它只会在标志为真时存储信息。这样,注册监听器就不会出现问题,但电池消耗可能会更高,因为只要应用程序正在运行,传感器就一直处于活动状态。

于 2013-03-27T06:26:05.793 回答