我有一个文本视图(Text1),当我在oncreate
中设置文本时更新正常但是当我尝试在onEvent方法中更新它时它没有更新我搜索了类似的帖子但是不能缩小什么导致我的具体问题
public class MainActivity extends Activity implements ProximityManager.ProximityListener {
private static final String TAG = MainActivity.class.getSimpleName();
private ProximityManagerContract proximityManager;
private ScanContext scanContext;
private TextView Text1;
private TextView Text2;
private TextView Text3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
KontaktSDK.initialize("").setDebugLoggingEnabled(BuildConfig.DEBUG)
.setLogLevelEnabled(LogLevel.DEBUG, true);
;
proximityManager = new KontaktProximityManager(this);
Text1 = (TextView) findViewById(R.id.textView);
Text2 = (TextView) findViewById(R.id.textView2);
}
@Override
protected void onStart() {
super.onStart();
proximityManager.initializeScan(getScanContext(), new OnServiceReadyListener() {
@Override
public void onServiceReady() {
proximityManager.attachListener(MainActivity.this);
EddystoneScanContext eddystoneScanContext = new EddystoneScanContext.Builder()
.setEventTypes(Arrays.asList(
EventType.SPACE_ENTERED,
EventType.DEVICES_UPDATE,
EventType.SPACE_ABANDONED))
.build();
ScanContext scanContext = new ScanContext.Builder()
.setEddystoneScanContext(eddystoneScanContext)
.build();
}
@Override
public void onConnectionFailure() {
}
});
}
@Override
protected void onStop() {
super.onStop();
proximityManager.detachListener(this);
proximityManager.disconnect();
}
@Override
public void onEvent(BluetoothDeviceEvent bluetoothDeviceEvent) {
final List<? extends RemoteBluetoothDevice> deviceList = bluetoothDeviceEvent.getDeviceList();
long timestamp = bluetoothDeviceEvent.getTimestamp();
DeviceProfile deviceProfile = bluetoothDeviceEvent.getDeviceProfile();
switch (bluetoothDeviceEvent.getEventType()) {
case SPACE_ENTERED:
Log.d(TAG, "namespace or region entered");
break;
case DEVICE_DISCOVERED:
Log.d(TAG, "found new beacon");
break;
case DEVICES_UPDATE:
Log.d(TAG, "updated beacons");
Log.d("Scan", String.valueOf(deviceProfile.toString()));
Log.d("List", deviceList.toString());
//Log.d("List", deviceProfile.name(deviceList.get(1)));
Log.d("distance", String.valueOf(deviceList.get(1).getDistance()));
Text1.setText("Is Updated");
break;
case DEVICE_LOST:
Log.d(TAG, "lost device");
break;
case SPACE_ABANDONED:
Log.d(TAG, "namespace or region abandoned");
break;
}
}
@Override
public void onScanStart() {
Log.d(TAG, "scan started");
}
@Override
public void onScanStop() {
Log.d(TAG, "scan stopped");
}
private ScanContext getScanContext() {
if (scanContext == null) {
scanContext = new ScanContext.Builder()
.setScanPeriod(ScanPeriod.RANGING) // or for monitoring for 15 seconds scan and 10 seconds waiting:
//.setScanPeriod(new ScanPeriod(TimeUnit.SECONDS.toMillis(15), TimeUnit.SECONDS.toMillis(10)))
.setScanMode(ProximityManager.SCAN_MODE_LOW_LATENCY)
.setActivityCheckConfiguration(ActivityCheckConfiguration.MINIMAL)
.setForceScanConfiguration(ForceScanConfiguration.MINIMAL)
.setIBeaconScanContext(new IBeaconScanContext.Builder().build())
.setEddystoneScanContext(new EddystoneScanContext.Builder().build())
.setForceScanConfiguration(ForceScanConfiguration.MINIMAL)
.build();
}
return scanContext;
}
}
答案 0 :(得分:0)
您无法更新主线程(UI线程)之外的视图。尝试使用runOnUiThread包装您的方法。
runOnUiThread(new Runnable() {
public void run() {
Text1.setText("Is Updated");
}
});
答案 1 :(得分:0)
title
您必须使用new Handler(new Runnable() {
public void run() {
Text1.setText("Is Updated");
}
});
或Handler
,因为您无法从外部编辑主线程中的组件