gpt4 book ai didi

android - 在 oncreate 方法中获取 NFC 唯一 ID

转载 作者:行者123 更新时间:2023-11-29 00:24:58 24 4
gpt4 key购买 nike

我试图在 on create 方法中获取 NFC 标签的唯一 ID。我已经使用名为“onNewIntent”的覆盖方法成功完成了此操作,但是我想在 oncreate 方法中获取它。这是我尝试过的,我手机上的应用程序在启动时崩溃了。请任何人帮忙。谢谢。

public class MainActivity extends Activity {

TextView uid;

private final String[][] techList = new String[][] {
new String[] {
NfcA.class.getName(),
NfcB.class.getName(),
NfcF.class.getName(),
NfcV.class.getName(),
NdefFormatable.class.getName(),
TagTechnology.class.getName(),
IsoDep.class.getName(),
MifareClassic.class.getName(),
MifareUltralight.class.getName(),
Ndef.class.getName()
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
uid = (TextView) findViewById(R.id.UID);
Intent i = new Intent();
IntentFilter filter = new IntentFilter();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);
if (i.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
((TextView)findViewById(R.id.UID)).setText(ByteArrayToHexString(i.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
}
}

private String ByteArrayToHexString(byte [] inarray) { //converts byte arrays to string
int i, j, in;
String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String out= "";

for(j = 0 ; j < inarray.length ; ++j)
{
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
}

最佳答案

Ok so basically i create a new intent. I get the default NFC hardware from the device. If a tag is discovered, it should change the textview to the ID of the NFC tag

为此,您所要做的就是设置新文本并调用 invalidate()onNewIntent() 中,以便 TextView 将使用新文本重新绘制:

package com.example.changetext;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView mNfcAdapterUid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mNfcAdapterUid = (TextView)findViewById(R.id.nfc_adapter_uid);
}

@Override
protected void onNewIntent(Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
mNfcAdapterUid.setText(ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
mNfcAdapterUid.invalidate();
}
}

private String ByteArrayToHexString(byte[] inarray) { // converts byte arrays to string
int i, j, in;
String[] hex = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
};
String out = "";

for (j = 0; j < inarray.length; ++j) {
in = inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
}

希望这对您有所帮助。

关于android - 在 oncreate 方法中获取 NFC 唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com