- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚接触 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+")");
}
}
}
}
}
}
这里,有两个问题:
结果告诉前台调度程序只捕获 TECH_DISCOVERED(有 techList)和 TAG_DISCOVERED(没有 techList),但错过了 NDEF_DISCOVERED。
当我离开应用程序并扫描 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_DISCOVERED
或 NDEF_DISCOVERED
),因为您的 Activity 已经收到任何已发现的标签。这同样适用于 TECH_DISCOVERED
与 Ndef
标签技术的结合:这已经包含了任何会触发 NDEF_DISCOVERED
的标签。但是,请注意 TAG_DISCOVERED
Intent 过滤器的特殊之处在于它在与前台调度一起使用时意味着“包罗万象”,而它意味着“仅后备”(即只有在没有更好的匹配时才匹配与任何其他应用程序)在基于 list 的 Intent 过滤器中使用时。
关于android - 为什么总是 TECH_DISCOVERED 或 TAG_DISCOVERED 而不是 NDEF_DISCOVERED?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26708009/
我正在尝试从 NFC 标签读取数据。如果我使用 Play Store 中的“NfcV-reader”应用程序(由标签制造商 ST Microelectronics 编写),那么我可以确认标签包含带有
自从我将 NFC 前台调度注册更改为使用 TECH_DISCOVERED Intent 过滤器后,我一直不得不在多个应用程序之间进行选择以处理 NFC 标签。当发现标签时,有没有办法在我的应用程序中直
我刚刚接触 Android 中的 NFC。我有几个问题。首先,让我介绍一下代码。在我的程序中,它只是简单地从记录中检索有效负载并将它们记录为字符串。 @Override protected void
我是一名优秀的程序员,十分优秀!