gpt4 book ai didi

android - 为什么总是 TECH_DISCOVERED 或 TAG_DISCOVERED 而不是 NDEF_DISCOVERED?

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

我刚刚接触 Android 中的 NFC。我有几个问题。首先,让我介绍一下代码。在我的程序中,它只是简单地从记录中检索有效负载并将它们记录为字符串。

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

adapter = NfcAdapter.getDefaultAdapter(this);
nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
nfcFilter = new IntentFilter[]{
new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED),
new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED),
new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)
};
techList = new String[][]{{Ndef.class.getName()}};
}

@Override
public void onResume(){
super.onResume();

if (adapter != null){
adapter.enableForegroundDispatch(this, nfcPendingIntent, nfcFilter, techList);
if (!adapter.isEnabled()){
Toast.makeText(this, "please enable your nfc", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "your device do not have nfc.", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onPause(){
super.onPause();
if (adapter != null){
adapter.disableForegroundDispatch(this);
}
}

@Override
protected void onNewIntent(Intent intent){
String TAG = "onNewIntent";
super.onNewIntent(intent);

Log.d(TAG, "action: "+intent.getAction());
//Log.d(TAG, "type: "+intent.getType());
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){
Parcelable[] rawMsg = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsg != null){
for (int i=0; i<rawMsg.length; i++){
NdefMessage msg = (NdefMessage)rawMsg[i];
NdefRecord[] records = msg.getRecords();
for (int j=0; j<records.length; j++){
Log.d(TAG, records[j].toMimeType()+"");
byte [] payload = records[j].getPayload();
if (payload != null && payload.length > 0){
Log.d(TAG, new String(payload, 1, payload.length-1));
}
}
}
}
} else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())){

} else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
Parcelable[] rawMsg = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsg != null){
for (int i=0; i<rawMsg.length; i++){
NdefMessage msg = (NdefMessage)rawMsg[i];
NdefRecord[] records = msg.getRecords();
for (int j=0; j<records.length; j++){
Log.d(TAG, records[j].toMimeType()+"");
byte [] payload = records[j].getPayload();
if (payload != null && payload.length > 0){
Log.d(TAG, new String(payload, 1, payload.length-1)+"("+j+")");
}
}
}
}
}
}

这里,有两个问题:

  1. 结果告诉前台调度程序只捕获 TECH_DISCOVERED(有 techList)和 TAG_DISCOVERED(没有 techList),但错过了 NDEF_DISCOVERED。

  2. 当我离开应用程序并扫描 NFC 标签时,它会自动将我带到该网站(我将 url 作为记录)。它如何告诉该记录包含打开浏览器或调用电话的操作?

最佳答案

NDEF_DISCOVERED Intent 过滤器通常(似乎存在一些异常(exception))仅在它具有与标签上的 NDEF 消息匹配的关联数据类型时才匹配。例如,数据类型规范 */* 将匹配任何 MIME 类型:

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {}
nfcFilter = new IntentFilter[]{
ndef,
new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED),
new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)
};

同样,如果您只想针对特定的 URL 触发 http://www.example.com/ ,你可以使用:

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
ndef.addDataScheme("http");
ndef.addDataAuthority("www.example.com", null);

请注意——使用前台调度系统——您通常只会注册您想要匹配的最通用的 Intent 过滤器。因此,如果您的前台调度 Intent 过滤器已经包含操作 TAG_DISCOVERED,则无需添加任何更具体的过滤器(如 TECH_DISCOVEREDNDEF_DISCOVERED ),因为您的 Activity 已经收到任何已发现的标签。这同样适用于 TECH_DISCOVEREDNdef 标签技术的结合:这已经包含了任何会触发 NDEF_DISCOVERED 的标签。但是,请注意 TAG_DISCOVERED Intent 过滤器的特殊之处在于它在与前台调度一起使用时意味着“包罗万象”,而它意味着“仅后备”(即只有在没有更好的匹配时才匹配与任何其他应用程序)在基于 list 的 Intent 过滤器中使用时。

关于android - 为什么总是 TECH_DISCOVERED 或 TAG_DISCOVERED 而不是 NDEF_DISCOVERED?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26708009/

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