我使用Runnable从GPS设备获取数据,但有时无法从外部获取数据,尽管内部的值总是有价值。
BaseSevice.java
protected Handler mHandler = new Handler();
protected volatile String mRawLine;
protected Runnable mReadDeviceDataTask = new Runnable() {
public void run() {
int delay = Constants.DEFAULT_GET_DATA_INTERVAL; //interval = 100ms
if (deviceType() == DeviceSupport.GPS) {
delay = Constants.GPS_GET_DATA_INTERVAL; //interval = 10ms
}
if (mDevice == null) {
mHandler.postDelayed(mReadDeviceDataTask, delay);
return;
}
try {
if (mInputStream.available() > 0) {
mRawLine = "some data here get from GPS device"; //data got continuously & many lines
}
} catch (IOException ioEx) {
throw new RuntimeException("Can not read lines from socket", ioEx);
} catch (Exception e) {
throw new RuntimeException("Error while read from input stream", e);
}
mHandler.postDelayed(mReadDeviceDataTask, delay);
}
};
GPSService.java 扩展了BaseService
public String getmRawLine() {
Log.e("debug","return mRawLine:" + mRawLine);
return mRawLine;//<--- sometimes there is no value
}