读取 NFC 标签直到它被分离并在读取标签时更新 UI
Read NFC tag until it is detached and update the UI while reading the tag
我想读取 NFC 标签,直到设备离开 NFC 标签。然后我想做一些活动。我已经设法使用 while 循环读取标签并捕获 InterruptedException 来做到这一点。而且我还想在读取 while 循环内的标签时更新 UI 。我在 while 循环中找不到更新 UI 的方法。
更新 UI 的数据来自 onLocationChanged 侦听器。
public void onLocationChanged(Location location) {
if (location.hasSpeed()) {
/*double speed=location.getSpeed() * 3.6;;
while (1==1)
{*/
speed = location.getSpeed() * 3.6;
String units="km/h";
s= new SpannableString(String.format(Locale.ENGLISH, "%.0f %s", speed, units));
s.setSpan(new RelativeSizeSpan(0.45f), s.length()-units.length()-1, s.length(), 0);
updateUI();
}
}
public void updateUI(){
drivingMode=findViewById(R.id.txtDriving);
currentSpeed = findViewById(R.id.valSpeed);
if (currentSpeed!=null) {
currentSpeed.setText(s);
if (speed > 10) {
drivingMode.setText(R.string.msg_driving);
isDriving = true;
} else {
drivingMode.setText(R.string.msg_notDriving);
isDriving=false;
}
}
}
private void readFromNFC( Ndef ndef) {
try
{
ndef.connect();
NdefMessage ndefMessage = ndef.getNdefMessage();
ndef.close();
String message = new String(ndefMessage.getRecords()[0].getPayload());
// Log.d(TAG, "readFromNFC Before Pass: " + message);
//Toast.makeText(this, "Text" + message, Toast.LENGTH_LONG).show();
if (message.equals("in")) {
Toast.makeText(this.getApplicationContext(), R.string.message_nfc_holder_detected, Toast.LENGTH_LONG).show();
if (isDialogshowing) {
dialog.dismiss();
isEmergency=false;
}
while (1 == 1) {
ndef.connect();
ndefMessage = ndef.getNdefMessage();
message = new String(ndefMessage.getRecords()[0].getPayload());
//Log.d(TAG, "readFromNFCPassed: " + message);
TimeUnit.SECONDS.sleep(1);
ndef.close();
updateUI();
}
} else {
Toast.makeText(this.getApplicationContext(), R.string.message_nfc_holder_error, Toast.LENGTH_LONG).show();
ndef.close();
}
} catch (IOException | FormatException | InterruptedException e ) {
e.printStackTrace();
Toast.makeText(this.getApplicationContext(), R.string.message_nfc_holder_detached, Toast.LENGTH_LONG).show();
if(isDriving) {
activateEmergency();
}
else
{
if (isDialogshowing) {
dialog.dismiss();
dialog.dismiss();
isDialogshowing = false;
}
}
}
}
为了连续读取 UI 线程而不阻塞主 UI 线程,您可以使用 AsyncTask。这还允许您使用 onProgressUpdate()
回调将状态更新发布到 UI(线程)。
您的 AsyncTask 可能看起来像这样:
private class ProcessNFCTask extends AsyncTask<Ndef, NdefMessage, Void> {
protected Void doInBackground(Ndef... tag) {
// This happens on the worker thread!
// TODO: content of readFromNFC(Ndef ndef);
// Instead of calling updateUI(), you would call publishProgress(message).
// You can use whatever represents your progress instead of "message".
// Instead of doing your finalization stuff (what you currently have
// in the catch clause) here, you would want to do that in
// onPostExecute() instead.
}
protected void onProgressUpdate(NdefMessage... progress) {
// This happens on the UI thread!
// You may also use the progress status information provided in
// "progress" here. This is what you pass to publishProgress(...).
updateUI();
}
protected void onPostExecute(Void result) {
// This happens on the UI thread!
// TODO: Whatever you want to do when you are finished reading, e.g.:
if (isDriving) {
activateEmergency();
} else {
if (isDialogshowing) {
dialog.dismiss();
dialog.dismiss();
isDialogshowing = false;
}
}
}
}
我想读取 NFC 标签,直到设备离开 NFC 标签。然后我想做一些活动。我已经设法使用 while 循环读取标签并捕获 InterruptedException 来做到这一点。而且我还想在读取 while 循环内的标签时更新 UI 。我在 while 循环中找不到更新 UI 的方法。
更新 UI 的数据来自 onLocationChanged 侦听器。
public void onLocationChanged(Location location) {
if (location.hasSpeed()) {
/*double speed=location.getSpeed() * 3.6;;
while (1==1)
{*/
speed = location.getSpeed() * 3.6;
String units="km/h";
s= new SpannableString(String.format(Locale.ENGLISH, "%.0f %s", speed, units));
s.setSpan(new RelativeSizeSpan(0.45f), s.length()-units.length()-1, s.length(), 0);
updateUI();
}
}
public void updateUI(){
drivingMode=findViewById(R.id.txtDriving);
currentSpeed = findViewById(R.id.valSpeed);
if (currentSpeed!=null) {
currentSpeed.setText(s);
if (speed > 10) {
drivingMode.setText(R.string.msg_driving);
isDriving = true;
} else {
drivingMode.setText(R.string.msg_notDriving);
isDriving=false;
}
}
}
private void readFromNFC( Ndef ndef) {
try
{
ndef.connect();
NdefMessage ndefMessage = ndef.getNdefMessage();
ndef.close();
String message = new String(ndefMessage.getRecords()[0].getPayload());
// Log.d(TAG, "readFromNFC Before Pass: " + message);
//Toast.makeText(this, "Text" + message, Toast.LENGTH_LONG).show();
if (message.equals("in")) {
Toast.makeText(this.getApplicationContext(), R.string.message_nfc_holder_detected, Toast.LENGTH_LONG).show();
if (isDialogshowing) {
dialog.dismiss();
isEmergency=false;
}
while (1 == 1) {
ndef.connect();
ndefMessage = ndef.getNdefMessage();
message = new String(ndefMessage.getRecords()[0].getPayload());
//Log.d(TAG, "readFromNFCPassed: " + message);
TimeUnit.SECONDS.sleep(1);
ndef.close();
updateUI();
}
} else {
Toast.makeText(this.getApplicationContext(), R.string.message_nfc_holder_error, Toast.LENGTH_LONG).show();
ndef.close();
}
} catch (IOException | FormatException | InterruptedException e ) {
e.printStackTrace();
Toast.makeText(this.getApplicationContext(), R.string.message_nfc_holder_detached, Toast.LENGTH_LONG).show();
if(isDriving) {
activateEmergency();
}
else
{
if (isDialogshowing) {
dialog.dismiss();
dialog.dismiss();
isDialogshowing = false;
}
}
}
}
为了连续读取 UI 线程而不阻塞主 UI 线程,您可以使用 AsyncTask。这还允许您使用 onProgressUpdate()
回调将状态更新发布到 UI(线程)。
您的 AsyncTask 可能看起来像这样:
private class ProcessNFCTask extends AsyncTask<Ndef, NdefMessage, Void> {
protected Void doInBackground(Ndef... tag) {
// This happens on the worker thread!
// TODO: content of readFromNFC(Ndef ndef);
// Instead of calling updateUI(), you would call publishProgress(message).
// You can use whatever represents your progress instead of "message".
// Instead of doing your finalization stuff (what you currently have
// in the catch clause) here, you would want to do that in
// onPostExecute() instead.
}
protected void onProgressUpdate(NdefMessage... progress) {
// This happens on the UI thread!
// You may also use the progress status information provided in
// "progress" here. This is what you pass to publishProgress(...).
updateUI();
}
protected void onPostExecute(Void result) {
// This happens on the UI thread!
// TODO: Whatever you want to do when you are finished reading, e.g.:
if (isDriving) {
activateEmergency();
} else {
if (isDialogshowing) {
dialog.dismiss();
dialog.dismiss();
isDialogshowing = false;
}
}
}
}