我有一个同时具有活动和服务的 android 应用程序。该服务从三星 Galaxy Gear 2 读取蓝牙传感器数据。 Galaxy Gear 以最快的速度输出数据,但在 Android 端接收数据存在令人难以置信的延迟。该服务是三行代码,所以它真的不能比这更有效。我认为这可能是我在下面编写的活动代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
//set global variables
x = (TextView)findViewById(R.id.xaccel);
y = (TextView)findViewById(R.id.yaccel);
z = (TextView)findViewById(R.id.zaccel);
doBindService(); //Conects to the Service
Thread myThread = new Thread(myRunnable);
myThread.start();
}
Runnable myRunnable = new Runnable() {
@Override
public void run() {
while (mIsBound) {
//check that the global variables have been set
if (x!=null && y!= null && z != null) {
x.post(new Runnable() {
@Override
public void run() {
if (HelloAccessoryProviderService.inputMessage != "") {
//read the service's global variable "inputMessage" and print it out
String mainData = HelloAccessoryProviderService.inputMessage;
x.setText("X: " + mainData.substring(0, 6));
y.setText("Y: " + mainData.substring(7, 13));
z.setText("Z: " + mainData.substring(14, 20));
}
};
});
}
}
}
};
我读到可运行文件是读取连续数据的最佳方式,但如果有其他方式,那就太好了。提前致谢!