我是Android的新手,现在我正在从事NFC项目,当smartcard
/ tag
检测到计时器将启动并且smartcard
中的“数据”具有被读取并显示给TextView的计时器将停止。我该怎么办?我只想使用1个TextView。因此,当TextView
像“ Hello World”一样从smartcard
获取数据时,计时器停止。
如果您也想修改我的编码,我非常感谢。真的很糟,抱歉。
这是停止计时器的方法:
TimeBuff += MillisecondTime;
handler.removeCallbacks(runnable);
我已经尝试过:
tvNFCContent.setText("NFC Content: " + text); // the textview show tag /smartcard data //
tvNFCContent = (TextView) findViewById(R.id.data);
tvNFCContent.getText().toString();
String yo = String.valueOf(yo.getText().toString());
if (yo == text){
TimeBuff += MillisecondTime;
handler.removeCallbacks(runnable);
}
public class Read extends Activity {
NfcAdapter mAdapter;
Tag mTag;
PendingIntent mPI;
IntentFilter mFilter[];
String userData, yo;
boolean writeMode;
Context context;
TextView tvNFCContent, Timer, Low;
Button start, pause, reset, lap;
long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L;
Handler handler;
int Seconds, Minutes, MilliSeconds;
ListView listView;
String[] ListElements = new String[]{};
List<String> ListElementsArrayList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read);
tvNFCContent = (TextView) findViewById(R.id.data);
Timer = (TextView) findViewById(R.id.timer);
handler = new Handler();
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPI = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter filter2 = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
mFilter = new IntentFilter[]{tagDetected, filter2};
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
}
readFromIntent(getIntent());
}
/**
* Read From NFC Tag
*
* @param intent
*/
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
buildTagViews(msgs);
}
}
private void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) return;
String text = "";
String tagId = new String(msgs[0].getRecords()[0].getType());
byte[] payload = msgs[0].getRecords()[0].getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en"
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
try {
text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncoding", e.toString());
}
tvNFCContent.setText("NFC Content: " + text);
}
NdefMessage[] getNdefMessage(Intent intent) {
NdefMessage[] msgs = null;
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
return msgs;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
setIntent(intent);
readFromIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
mTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
super.onNewIntent(intent);
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Toast.makeText(getApplicationContext(), "Ndefdiscovered", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
mTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(getApplicationContext(), "Smartcard detected", Toast.LENGTH_SHORT).show();
StartTime = SystemClock.uptimeMillis();
handler.postDelayed(runnable, 0);
NdefMessage[] messages = getNdefMessage(intent);
if (messages == null) {
Toast.makeText(getApplicationContext(), "Data di dalam kartu kosong", Toast.LENGTH_SHORT).show();
return;
}
byte[] payload = messages[0].getRecords()[0].getPayload();
userData = new String(payload);
} else {
Toast.makeText(getApplicationContext(), "Undefined smartcard", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mAdapter.disableForegroundDispatch(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mAdapter.enableForegroundDispatch(this, mPI, mFilter, null);
}
public Runnable runnable = new Runnable() {
public void run() {
MillisecondTime = SystemClock.uptimeMillis() - StartTime;
UpdateTime = TimeBuff + MillisecondTime;
Seconds = (int) (UpdateTime / 1000);
Minutes = Seconds / 60;
Seconds = Seconds % 60;
MilliSeconds = (int) (UpdateTime % 1000);
Timer.setText("" + Minutes + ":"
+ String.format("%02d", Seconds) + ":"
+ String.format("%03d", MilliSeconds));
handler.postDelayed(this, 0);
}
};
}
我不想使用按钮。只是自动的,当TextView显示标签/智能卡数据时,计时器将停止。
答案 0 :(得分:0)
首先,当您比较两个字符串时。您需要使用<p>
。
因此,equals()
必须这样更改:
if(yo == text)
第二,您需要在使用if(yo.equals(text)){
TimeBuff += MillisecondTime;
handler.removeCallbacks(runnable);
}
之前声明tvNFCContent
。
因此,订单必须为:
setText()
我不能完全理解你,但是。
尝试一下:
tvNFCContent = (TextView) findViewById(R.id.data); // This must come first.
tvNFCContent.setText("NFC Content: " + text); // Then you can set the text.
// tvNFCContent.getText().toString(); // this part is not necessary because you already have "text" variable somewhere.
String yo = String.valueOf(yo.getText().toString()); // Duplicate names are not a good naming. Try using "yoStr"(String) and "tvYo"(TextView) or something like that.