我一直试图通过TCP Socket连接将我的Android手机传感器数据发送到我的PC。我的目标是刷新10-40毫秒,我正在研究手机传感器在测量技术方面的潜在应用,这些测量技术包括将手机放在秋千上,并使用手机的传感器测量加速度。Android定时器不低于〜120ms分辨率
这里是我的问题:但是我尝试通过套接字发送消息,我不能低于约120ms的刷新率。如果我没有设置任何延迟,使用一个简单的while(true)循环,它会快速发展(如预期的那样),但是当我尝试实现任何类型的延迟时(从1ms到100ms,无所谓),延迟只是不会不会低于〜120ms。
这里是什么,我到目前为止已经试过一些例子:
//outside of onCreate()
final long refreshRate=10;//millisecods
long time;
//in onCreate()
//...
time=System.currentTimeMillis();//initial setup
//...
dataBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isSendingData){
isSendingData=true;
dataBtn.setText("Stop sending data");
new Thread(){
public void run(){
while(isConnected&&isSendingData&&mTcpClient!=null){//TODO
long currentTime=System.currentTimeMillis();
if(currentTime-time>=refreshRate){
String sx=String.format(Locale.GERMANY,"%3.2f", x);
String sy=String.format(Locale.GERMANY,"%3.2f", y);
String sz=String.format(Locale.GERMANY,"%3.2f", z);
try{
mTcpClient.sendMessage(sx+";"+sy+";"+sz+";");//TODO trycatch testing
}catch(Exception e){
isSendingData=false;
e.printStackTrace();
}
time=currentTime;
}
}
}
}.start();
}else{
isSendingData=false;
dataBtn.setText("Start sending data");
}
}
});
其他例如:
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
while(isConnected&&isSendingData&&mTcpClient!=null){
String sx=String.format(Locale.GERMANY,"%3.2f", x);
String sy=String.format(Locale.GERMANY,"%3.2f", y);
String sz=String.format(Locale.GERMANY,"%3.2f", z);
try{
mTcpClient.sendMessage(sx+";"+sy+";"+sz+";");//TODO trycatch testing
}catch(Exception e){
isSendingData=false;
e.printStackTrace();
}
}
if(!isSendingData||!isConnected||mTcpClient==null)this.cancel();
}
}, 10, 10);
+0
http://developer.android.com/reference/android/os/SystemClock.html中的任何时钟都更好吗? –