我创建了一个Activity来创建文件,并在传感器更改时向其写入传感器数据。我的代码如下:
public class AllSensors extends ActionBarActivity implements SensorEventListener{
Long startTime;
Long endTime;
Long currentTime;
private float ax,ay,az,gx,gy,gz,mx,my,mz;
Button start,stop,save;
private SensorManager sensorManager;
private Sensor accelerometer;
private Sensor gyroscope;
private Sensor magnetometer;
String content = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_sensors);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
save = (Button) findViewById(R.id.save);
Typeface scribble = Typeface.createFromAsset(getAssets(), "fonts/warning.otf");
start.setTypeface(scribble);
start.setTextSize(80);
stop.setTypeface(scribble);
stop.setTextSize(80);
save.setTypeface(scribble);
save.setTextSize(80);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// register all the sensors
if(sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) !=null ){
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
}
if(sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) !=null ){
gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorManager.registerListener(this,gyroscope,SensorManager.SENSOR_DELAY_NORMAL);
}
if(sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) !=null ){
magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(this,magnetometer,SensorManager.SENSOR_DELAY_NORMAL);
}
}
然后在saveData方法中我创建了一个文件(这个方法是按钮的onClick方法,当用户点击它时,它会创建并写入内容):
try{
file = new File(Environment.getExternalStorageDirectory(),filename);
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(title.getBytes());
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
}catch (IOException e) {
Toast.makeText(AllSensors.this,"An error occurred!",Toast.LENGTH_SHORT).show();
return;
}
在onSensorChanged方法中,当我取消注释当前注释的代码时,应用程序将编译但会在运行时停止:
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
currentTime = System.currentTimeMillis();
//if(currentTime >= startTime && currentTime <= endTime) {
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
ax = event.values[0];
ay = event.values[1];
az = event.values[2];
} else if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
gx = event.values[0];
gy = event.values[1];
gz = event.values[2];
} else if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mx = event.values[0];
my = event.values[1];
mz = event.values[2];
}
//content += Long.toString(currentTime-startTime) + " " + Float.toString(ax) + " "
// + Float.toString(ay) + " " + Float.toString(az) + " " + Float.toString(mx) + " "
//+ Float.toString(my) + " " + Float.toString(mz) + " " + Float.toString(gx) + " "
//+ Float.toString(gy) + " " + Float.toString(gz) + "/n";
// }
}
有谁知道它为什么?最初我只是写了内容+ = ax + ay ....但它也会在运行时停止。
谢谢!
答案 0 :(得分:0)
我自己解决了。问题是我将字段startTime,currentTime和endTime声明为Long而不是long。因此,比较和字符串追加失败。