在Android中执行线程后,如何拥有最新的变量?
我在Android应用程序中使用蓝牙,所以在onTouchEvent中,我运行startDiscovery。然后,在onReceive中,我更改一些属性的值。回到onTouchEvent,我使用System.sleep8000等待打印刚刚获得的值。 我可以看到,这些值都经过了很好的修改,但有一个调用gap。我的意思是,打印的是上一次调用onTouchEvent的值 您知道如何获取上次调用的实际值吗?多谢各位 找到蓝牙设备时调用的函数:在Android中执行线程后,如何拥有最新的变量?,android,multithreading,bluetooth,attributes,touch-event,Android,Multithreading,Bluetooth,Attributes,Touch Event,我在Android应用程序中使用蓝牙,所以在onTouchEvent中,我运行startDiscovery。然后,在onReceive中,我更改一些属性的值。回到onTouchEvent,我使用System.sleep8000等待打印刚刚获得的值。 我可以看到,这些值都经过了很好的修改,但有一个调用gap。我的意思是,打印的是上一次调用onTouchEvent的值 您知道如何获取上次调用的实际值吗?多谢各位 找到蓝牙设备时调用的函数: public void onReceive(Context
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
amp = 0;
name = null;
if (device.getAddress().equals("00:11:22:33:44")) {
amp = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
name = device.getName();
}
}
}
在onTouchEvent中:
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
mBluetoothAdapter.startDiscovery();
SystemClock.sleep(8000);
print_RSSI(x, y);
在此之后调用以打印的函数:
private void print_RSSI(float x, float y) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.Rel1);
TextView tv;
RelativeLayout.LayoutParams params;
tv = new TextView(this);
tv.setText(name + ":" + String.valueOf(amp));
params = new RelativeLayout.LayoutParams(200, 40);
params.leftMargin = (int) x;
params.topMargin = (int) y;
rl.addView(tv, params);
}