我的代码曾经用于2.2,我能够获得陀螺仪数据,但在更新到2.3后,它不再起作用了。我还试过一个Galaxy播放器来确保它不是我手机的硬件问题。它也不适用于那个。有什么建议?这是代码:
package my.acc;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.ToggleButton;
public class myact extends Activity {
private SensorManager mSensorManager;
Sensor myAcc, myAcc2, myAcc3, myAcc4;
MySensorListener listener, listener2,listener3, listener4;
EditText mEdit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.activity_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
// Set up the accelerometer reading
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
myAcc = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
myAcc2 = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
myAcc3 = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
myAcc4 = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
mEdit = (EditText)findViewById(R.id.entry);
togglebutton.setOnClickListener(new OnClickListener() {
PrintWriter mCurrentFile = null;
PrintWriter mCurrentFile2 = null;
PrintWriter mCurrentFile3 = null;
PrintWriter mCurrentFile4 = null;
public void onClick(View v) {
// Perform action on clicks
if (togglebutton.isChecked()) {
String strname = spinner.getSelectedItem().toString();
if (strname.equals("Select Activity")){
strname = mEdit.getText().toString();
}
String nameStr = new String("/sdcard/" + strname + "acc" + ".csv");
String nameStr2 = new String("/sdcard/" + strname + "gyr" + ".csv");
String nameStr3 = new String("/sdcard/" + strname + "ori" + ".csv");
String nameStr4 = new String("/sdcard/" + strname + "gra" + ".csv");
File outputFile = new File(nameStr);
File outputFile2 = new File(nameStr2);
File outputFile3 = new File(nameStr3);
File outputFile4 = new File(nameStr4);
try {
mCurrentFile = new PrintWriter(new FileOutputStream(outputFile));
mCurrentFile2 = new PrintWriter(new FileOutputStream(outputFile2));
mCurrentFile3 = new PrintWriter(new FileOutputStream(outputFile3));
mCurrentFile4 = new PrintWriter(new FileOutputStream(outputFile4));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mSensorManager.registerListener(listener = new MySensorListener(mCurrentFile), myAcc , SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(listener2 = new MySensorListener(mCurrentFile2), myAcc2 , SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(listener3 = new MySensorListener(mCurrentFile3), myAcc3 , SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(listener4 = new MySensorListener(mCurrentFile4), myAcc4 , SensorManager.SENSOR_DELAY_UI);
//mSensorManager.registerListener(listener, myAcc2 , SensorManager.SENSOR_DELAY_FASTEST);
} else {
mSensorManager.unregisterListener(listener, myAcc);
mSensorManager.unregisterListener(listener2, myAcc2);
mSensorManager.unregisterListener(listener3, myAcc3);
mSensorManager.unregisterListener(listener4, myAcc4);
mCurrentFile.close();
mCurrentFile2.close();
mCurrentFile3.close();
mCurrentFile4.close();
}
}
});
}
}
package my.acc; import java.io.PrintWriter; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; public class MySensorListener implements SensorEventListener { String comma = new String(","); private PrintWriter mCurrentFile; public MySensorListener( PrintWriter mCurrentFile){ //Creating a file to print the data into this.mCurrentFile = mCurrentFile; } public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } public void onSensorChanged(SensorEvent event) { StringBuffer buff = new StringBuffer(); buff.append(String.valueOf(event.sensor.getName())); buff.append(comma); buff.append(String.valueOf(event.timestamp)); buff.append(comma); buff.append(String.valueOf(event.values[0])); buff.append(comma); buff.append(String.valueOf(event.values[1])); buff.append(comma); buff.append(String.valueOf(event.values[2])); mCurrentFile.println(buff.toString()); mCurrentFile.flush(); } }
我甚至报道谷歌这里的一个错误就是他们的反应:
这些不是Google设备,因此您必须与三星交谈
答案 0 :(得分:1)
我不确定这是否有帮助,但它似乎与您的问题有关:陀螺仪无法闪烁rom。您可以查看(Galaxy S2 - Gyro & Compass not working)并尝试运行建议的测试。
答案 1 :(得分:0)
似乎只是一个三星问题。向Google提出问题没有帮助;他们把我转回三星。