扫描NFC标签时自定义应用未启动,系统NFC服务启动,需排查原因
NFC标签扫描未唤起自定义应用的问题排查(Pixel 8)
已完成配置回顾
1. Manifest权限与特性声明
<uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />
2. Activity的Intent Filter与元数据配置
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> <data android:scheme="https" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/vnd.com.example" /> </intent-filter> <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
3. NFC技术过滤XML配置(res/xml/nfc_tech_filter.xml)
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <tech-list> <tech>android.nfc.tech.IsoDep</tech> <tech>android.nfc.tech.NfcA</tech> <tech>android.nfc.tech.NfcB</tech> <tech>android.nfc.tech.NfcF</tech> <tech>android.nfc.tech.NfcV</tech> <tech>android.nfc.tech.Ndef</tech> <tech>android.nfc.tech.NdefFormatable</tech> <tech>android.nfc.tech.MifareClassic</tech> <tech>android.nfc.tech.MifareUltralight</tech> </tech-list> </resources>
问题原因与修复方案
1. 修正TECH_DISCOVERED过滤规则逻辑
当前的tech-list配置要求标签同时支持所有列出的NFC技术才会触发,这几乎不可能匹配任何真实标签。正确逻辑应为:单个tech-list内的技术是「与」关系,多个tech-list是「或」关系,需为每种技术单独创建tech-list:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <tech-list> <tech>android.nfc.tech.IsoDep</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NfcA</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NfcB</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NfcF</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NfcV</tech> </tech-list> <tech-list> <tech>android.nfc.tech.Ndef</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NdefFormatable</tech> </tech-list> <tech-list> <tech>android.nfc.tech.MifareClassic</tech> </tech-list> <tech-list> <tech>android.nfc.tech.MifareUltralight</tech> </tech-list> </resources>
2. 适配Android 12+前台调度限制
Pixel 8运行Android 12及以上系统,系统限制后台应用接收NFC Intent,需在Activity中添加前台调度逻辑:
private NfcAdapter mNfcAdapter; @Override protected void onResume() { super.onResume(); mNfcAdapter = NfcAdapter.getDefaultAdapter(this); if (mNfcAdapter != null && mNfcAdapter.isEnabled()) { Intent intent = new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE); mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null); } } @Override protected void onPause() { super.onPause(); if (mNfcAdapter != null) { mNfcAdapter.disableForegroundDispatch(this); } }
注:Android 12+要求PendingIntent指定FLAG_MUTABLE或FLAG_IMMUTABLE,根据业务需求选择。
3. 验证NDEF标签数据匹配度
若扫描的是NDEF格式标签,需确保标签数据与NDEF_DISCOVERED规则严格匹配:
- HTTP/HTTPS链接:标签内必须是
UriRecord,scheme严格为http/https,无额外参数或路径导致匹配失败; - 自定义MIME类型:标签内必须是
MimeRecord,MIME类型严格等于application/vnd.com.example,大小写、后缀均不能出错。
4. 设置应用为NFC默认处理程序
进入Pixel 8系统设置:设置 → 应用 → 选择你的应用 → 打开为默认应用 → NFC,将应用设为对应NFC类型的默认处理应用,避免其他应用抢占优先级。
内容的提问来源于stack exchange,提问作者Waza_Be
相关产品推荐
相关产品推荐

