如何仅在指定Android Activity中实现NFC标签扫描?已尝试方案致应用无法启动
问题描述
我希望仅在选定的AddingCostumesActivity中实现NFC标签扫描功能,目前我的应用在所有Activity中,只要手机靠近NFC标签就会触发扫描。尝试调整配置后应用完全无法启动,以下是我的AndroidManifest.xml代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.nfc_scanner"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.NFC_Scanner" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true" tools:ignore="WrongManifestParent"> <intent-filter> <!-- MAIN represents that it is the Main Activity--> <action android:name="android.intent.action.MAIN" /> <!-- Launcher Denotes that it will be the first launching activity--> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".RentActivity" tools:ignore="WrongManifestParent"> </activity> <activity android:name=".AddingCostumesActivity" android:launchMode="singleTask" android:exported="true"> <intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED"/> </intent-filter> <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/tech_list" /> </activity> </application> <uses-permission android:name="android.permission.NFC" tools:ignore="ManifestOrder" /> </manifest>
解决方案
1. 修复应用无法启动的问题
你的配置存在两个核心问题导致启动失败:
- 移除误用的
tools:ignore="WrongManifestParent":该标签用于布局文件的错误提示,放在Activity标签下属于无效配置,直接删除即可。 - 确保
tech_list.xml文件存在且格式正确:TECH_DISCOVERED元数据需要指定支持的NFC技术列表,若文件缺失或格式错误会直接导致应用崩溃。在res/xml/目录下创建tech_list.xml,示例内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <tech-list> <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-list> </resources>
2. 实现仅指定Activity触发NFC扫描
方式一:静态配置+前台分发控制
保留AddingCostumesActivity中的NFC intent-filter和元数据,同时通过代码动态控制NFC事件的分发优先级,确保只有当前Activity处于前台时才响应NFC:
在AddingCostumesActivity中添加以下代码:
@Override protected void onResume() { super.onResume(); NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null) { // 创建PendingIntent,确保NFC事件发送到当前Activity PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_MUTABLE); // 启用前台分发,优先让当前Activity处理NFC事件 nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null); } } @Override protected void onPause() { super.onPause(); NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null) { // 暂停时禁用前台分发,避免后台触发NFC nfcAdapter.disableForegroundDispatch(this); } }
方式二:完全动态控制(推荐)
移除Manifest中所有NFC相关的intent-filter和元数据,完全通过代码控制NFC扫描的开启与关闭:
- 仅在
AddingCostumesActivity的onResume()中启用前台分发(代码同上)。 - 在
onPause()中禁用前台分发(代码同上)。 - 其他Activity不做任何NFC相关处理,这样只有当
AddingCostumesActivity处于前台时才会响应NFC标签。
最终修正后的AndroidManifest.xml示例
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.nfc_scanner"> <uses-permission android:name="android.permission.NFC" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.NFC_Scanner" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".RentActivity" /> <activity android:name=".AddingCostumesActivity" android:launchMode="singleTask" android:exported="true" /> </application> </manifest>
内容的提问来源于stack exchange,提问作者bastekrjn
相关产品推荐
相关产品推荐

