android - NFC类型默认应用

标签 android android-intent nfc intentfilter ndef

我刚开始使用 NFC。扫描 NFC 标签时,我的应用默认打开,但我有 2 个问题:

  1. 出于某种原因,我无法在启动器应用浏览器中看到我的应用。

  2. 如何定义只在NFC为URL类型时打开应用?

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    
            <activity android:name=".MainActivity">
            <intent-filter>
    
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
    
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
    </application>
    

最佳答案

第一个问题(应用程序未显示在启动器中)是由于多个过滤条件组合在一个条件中的方式造成的 <intent-filter>部分(参见 Intents and Intent Filters 。您可以通过将 Intent 过滤器拆分为单独的 <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:mimeType="text/plain" />
</intent-filter>

第二个问题取决于您在标签上存储的数据(NDEF 记录)。例如,如果您存储了包含 URL https://stackoverflow.com/ 的 URI 记录,您将使用以下 Intent 过滤器:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="https" android:host="stackoverflow.com" />
</intent-filter>

你可能还想检查一下

关于android - NFC类型默认应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48497612/

相关文章:

java - 我正在尝试编写谷歌地图应用程序代码,但即使在安装谷歌游戏商店服务后,我也收到以下错误:

android - 如何区分 Android 中的主要配置文件和次要配置文件?

android - 在退出应用程序时结束 SurfaceView 和 GameThread

java - 单击时使 RecyclerView 图像全屏显示

android - fragment 更换后抽屉切换不动画

android - 如何在 Android 上管理 startActivityForResult

java - Android 比例动画 - 反转问题

android - 打开 nfc 示例应用程序

android - 从 NFC 标签读取 URI 时丢失数据

android - P2P通信/LLCP NFC读写器-NFC手机