我的 android 项目(传感器捕获、多线程处理和输出写入 csv 文件)在 Nexus 和其他设备(4.4 kitkat、4.3 / 4.2 Jellybean)上运行良好……但在 Sony Xperia Z 上测试时,它崩溃了。 .
提示:在调试模式下运行不会崩溃....
我猜这与多线程问题有关,因为 dat 被捕获、处理并写入输出 csv 文件,并且在运行几秒钟后发生崩溃......
有什么特定于我忘记的索尼实施的吗?
LogCat 可以在以下位置找到: https ://gist.github.com/erwin/10535096
感谢您的任何提示....我需要让它在索尼设备上运行...
这是将启动 2 个线程的主要 SwimActivity:samplingThread(将传感器数据捕获到捕获列表中)
public class SwimActivity extends Activity implements OnSeekBarChangeListener {
private SamplingThread samplingThread;
private InterpolationThread interpolationThread;
public volatile ArrayList<LinkedList<CapturedEvent>> captures = new ArrayList<LinkedList<CapturedEvent>>(); // filled by sampligThread
public volatile ArrayList<LinkedList<CapturedEvent>> sensors = new ArrayList<LinkedList<CapturedEvent>>();
public volatile LinkedList<InterpolatedEvent> cachedData = new LinkedList<InterpolatedEvent>();
class SwimHandler extends Handler { // handle messages from interpolationThread
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (this != null) {
Bundle bundle = msg.getData();
Integer msgKey = bundle.getInt("msgKey");
switch(msgKey){
case SENSORS_READY:
showToast("all sensors ready..");
sensorsReady();
break;
case SAMPLING:
showToast("sampling..");
break;
case SAMPLING_COMPLETED:
samplingCompleted();
break;
}
}
}
}
public final SwimHandler mHandler = new SwimHandler();
private void startSampling() { // user hit the start button on UI
// init output csv file
// ....
FileWriter fileWriter = new FileWriter( captureFileName, false );
captureFile = new PrintWriter( fileWriter );
// initialize ArrayList
// ........
Log.i(TAG, "starting samplingThread");
samplingThread = new SamplingThread(this, captures);
samplingThread.setPriority( Thread.NORM_PRIORITY);
samplingThread.start();
}
private void sensorsReady() { // when message 'SENSORS_READY' received from interpolationThread
cachedData.clear();
Log.i(TAG, "sensors ready, starting interpolationThread");
interpolationThread = new InterpolationThread(SwimActivity.this, captures, sensors, cachedData, ... other params );
interpolationThread.setPriority( Thread.NORM_PRIORITY + 1);
interpolationThread.start();
}
}
和插值线程(处理捕获的数据并将插值数据写入 csv 文件)
public class InterpolationThread extends Thread {
// .....
ArrayList<LinkedList<CapturedEvent>> captures;
ArrayList<LinkedList<CapturedEvent>> sensors;
LinkedList<InterpolatedEvent> cachedData;
public InterpolationThread(SwimActivity activity, ArrayList<LinkedList<CapturedEvent>> ceList ..... {
// ... init params
running = true;
}
@Override
public void run() {
lastMessageTime = SystemClock.elapsedRealtime(); // millis
if (interpolationTime == 0 ) { setInitialInterpolationTime(); }
lastMessageTime = SystemClock.elapsedRealtime(); // millis
if (interpolationTime == 0 ) { setInitialInterpolationTime(); }
while(running ){
interpolatedSensorData = interpolateAllSensors(); // interpolate && set sensorStatus - Vector<Integer>
addInterpolatedDataToCache(interpolatedSensorData);
processCachedData();
cleanUpCachedData(); // write completed interpolations into csv log file
}
running = false;
}
public void interrupt() {
running = false;
if( captureFile != null )
captureFile.close();
}
// other processing methods
public void cleanUpCachedData() {
ListIterator<InterpolatedEvent> cacheIterator = cachedData.listIterator();
while(cacheIterator.hasNext()) {
InterpolatedEvent cachedEvent = cacheIterator.next();
Vector<Vector3> cachedEventValues = cachedEvent.values; // all sensors
if (allInterpolationsCompleted(cachedEventValues)) { // all sensors got interpolated data
writeInterpolatedDataIntoLogFile(cachedEventValues);
cacheIterator.remove(); // remove current interpolatedEvent from cache()
}
}
}
public void writeInterpolatedDataIntoLogFile(Vector<Vector3>interpolatedSensorData) {
// .....
elapsedLogTime += samplingRate;
Vector3 orientation = calculateOrientation(interpolatedSensorData);
// ...
String data = "" + elapsedLogTime;
Iterator<Vector3> sensorIterator = interpolatedSensorData.iterator();
while(sensorIterator.hasNext() ) {
Vector3 values = (Vector3) sensorIterator.next();
int sensorIndex = interpolatedSensorData.indexOf(values);
if (sensorIndex == 1 && !gyro) {
Vector3 zeroValues = new Vector3(); // insert gyro = zero
for (int i = 0; i < 3; i++) { data = data + ";" + nf.format(zeroValues.toArray()[i]);} //gyro 0
for (int i = 0; i < 3; i++) { data = data + ";" + nf.format(zeroValues.toArray()[i]);} // linearAccel 0
} else if (sensorIndex == 2 && !linearAccel) {
Vector3 zeroValues = new Vector3(); // insert gyro = zero
for (int i = 0; i < 3; i++) { data = data + ";" + nf.format(zeroValues.toArray()[i]);}
for (int i = 0; i < 3; i++) { data = data + ";" + nf.format(values.toArray()[i]); } // add linearAccel data
}
else
for (int i = 0; i < 3; i++) { data = data + ";" + nf.format(values.toArray()[i]); }
}
for (int i = 0; i < 3; i++) { data = data + ";" + nf.format(orientation.toArray()[i]); }
captureFile.println( data );
}
}
}