0

ログファイルに加速度計データを記録する必要があるプロジェクトを行っています。誰でも次のコードで私を助けることができますか? 加速度計データを記録するために次のコードに追加する必要がある Android コードは何ですか? 10ミリ秒ごとにデータを取得したいと思うかもしれません。どんな助けでも大歓迎です。

package com.example.helloandroid;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AccActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;

TextView xCoor; // declare X axis object
TextView yCoor; // declare Y axis object
TextView zCoor; // declare Z axis object

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
    yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
    zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object

    sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
    // add listener. The listener will be HelloAndroid (this) class
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);

    /*  More sensor speeds (taken from api docs)
        SENSOR_DELAY_FASTEST get sensor data as fast as possible
        SENSOR_DELAY_GAME   rate suitable for games
        SENSOR_DELAY_NORMAL rate (default) suitable for screen       orientation changes
    */
}

public void onAccuracyChanged(Sensor sensor,int accuracy){

}

public void onSensorChanged(SensorEvent event){

    // check sensor type
    if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

        // assign directions
        float x=event.values[0];
        float y=event.values[1];
        float z=event.values[2];

        xCoor.setText("X: "+x);
        yCoor.setText("Y: "+y);
        zCoor.setText("Z: "+z);
    }
}

}

4

1 に答える 1

1

データはコールバックをトリガーするイベントによって配信されるため、データを受信する速度はオペレーティング システムの裁量に依存します。また、デバイスに依存する可能性もあります。あらかじめ決められたレートでセンサーをポーリングすることはできません。SensorManager API ドキュメントの状態

public boolean registerListener (SensorEventListener listener, Sensor sensor, int rate)

レート センサー イベントが配信されます。これはシステムへのヒントにすぎません。イベントは、指定された速度より速くまたは遅く受信される場合があります。通常、イベントはより速く受信されます。値は、SENSOR_DELAY_NORMAL、SENSOR_DELAY_UI、SENSOR_DELAY_GAME、または SENSOR_DELAY_FASTEST のいずれか、またはマイクロ秒単位のイベント間の目的の遅延である必要があります。

したがって、10,000 マイクロ秒 (10 ミリ秒) の遅延を要求できますが、それが onSensorChanged に配信される頻度は、それを測定する実験によってのみ決定できます。

于 2012-05-02T19:58:36.180 に答える