You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter Android设备上用nfc_manager阻止默认“New Tag Scanned”弹窗?

解决nfc_manager插件扫描NFC时Android弹出系统“New Tag Scanned”弹窗的问题

这个弹窗是Android系统默认的NFC调度提示,要阻止它,核心是让你的应用获得NFC前台调度优先级,优先处理NFC标签事件,不让系统触发默认弹窗。以下是具体实现步骤:

1. 配置AndroidManifest.xml

在android/app/src/main/AndroidManifest.xml中添加必要权限、功能声明和Intent过滤器,确保应用能接收NFC标签事件:

<!-- NFC权限 -->
<uses-permission android:name="android.permission.NFC" />
<!-- 声明设备需要NFC硬件 -->
<uses-feature android:name="android.hardware.nfc" android:required="true" />

<application ...>
  <activity
      android:name=".MainActivity"
      ...>
      <!-- 拦截NDEF类型标签 -->
      <intent-filter>
          <action android:name="android.nfc.action.NDEF_DISCOVERED" />
          <category android:name="android.intent.category.DEFAULT" />
          <!-- 匹配URL类型NDEF标签,可根据你的标签类型调整 -->
          <data android:scheme="https" />
      </intent-filter>
      <!-- 拦截特定NFC技术类型标签 -->
      <intent-filter>
          <action android:name="android.nfc.action.TECH_DISCOVERED" />
      </intent-filter>
      <meta-data
          android:name="android.nfc.action.TECH_DISCOVERED"
          android:resource="@xml/nfc_tech_filter" />
  </activity>
</application>

然后在res/xml目录下创建nfc_tech_filter.xml文件,定义需要支持的NFC技术类型:

<resources>
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <!-- 可添加其他需要支持的NFC技术,如MifareClassic等 -->
    </tech-list>
</resources>

2. 使用nfc_manager的前台调度功能

在Flutter代码中,通过startSession配置Android前台调度选项,让应用优先处理NFC标签:

import 'package:nfc_manager/nfc_manager.dart';

// 启动NFC会话,阻止系统弹窗
Future<void> startNfcForegroundDispatch() async {
  bool isNfcAvailable = await NfcManager.instance.isAvailable();
  if (!isNfcAvailable) return;

  await NfcManager.instance.startSession(
    onDiscovered: (NfcTag tag) async {
      // 在这里处理扫描到的NFC标签
      Ndef? ndef = Ndef.from(tag);
      if (ndef != null && ndef.cachedMessage != null) {
        // 读取NDEF数据示例
        NdefMessage message = ndef.cachedMessage!;
        for (var record in message.records) {
          String content = String.fromCharCodes(record.payload.sublist(3));
          // 处理读取到的内容
        }
      }
      // 如需持续扫描,不要调用stopSession;否则处理完后调用结束会话
      // await NfcManager.instance.stopSession();
    },
    androidSessionOptions: const AndroidSessionOptions(
      // 设置前台调度模式,确保应用优先处理NFC事件
      foregroundDispatchMode: AndroidForegroundDispatchMode.read,
      // 可添加标签过滤,只处理特定类型的NFC标签
      filters: [
        NdefFilter(
          type: NdefTypeNameFormat.nfcWellKnown,
          typeName: 'U', // 匹配URL类型NDEF标签
        ),
      ],
    ),
  );
}

在页面初始化时启动NFC会话,销毁时停止:

@override
void initState() {
  super.initState();
  startNfcForegroundDispatch();
}

@override
void dispose() {
  NfcManager.instance.stopSession();
  super.dispose();
}

原理说明

通过配置AndroidManifest的Intent过滤器,让应用能匹配到NFC标签事件;再通过nfc_manager的foregroundDispatchMode参数,向Android系统申请前台调度优先级,系统会将NFC标签事件直接发送给你的应用,不再触发默认的“New Tag Scanned”弹窗。

内容的提问来源于stack exchange,提问作者maybrother

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 08:37:52