在连接的任何(空)NFC 上启动应用程序

Start app on whatever (empty) NFC attached

我已经尝试了大约 100 种不同的方法来完成它。我只是不知道该怎么做。

我有一个空的 NFC 标签。如果我的 phone 扫描了 NFC 标签,我只需要启动应用程序,无论它是什么标签,或者上面写的是什么。我不想在上面写东西,也不想看。

目前,如果我扫描 NFC 标签,我的 phone 会打开 Android 内置 NFC reader。

如果打开我的应用程序,它会检测到扫描 NFC 标签。但是我想通过扫描标签启动应用程序。

这是我的清单:

<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <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>

    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <action android:name="android.nfc.action.TAG_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

请注意,我尝试了不同的方式来启动该应用程序...它一次都不起作用。

如果您需要我的 MainActivity,请告诉我。请注意,我的 MainActivity 并未在启动时启动。我前面有一个闪屏activity。

这可能吗?而且,是否可以通过服务或类似方式启动应用程序?

我创建了一个简单的应用程序,我只将清单文件更改为:

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <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.TAG_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

    </activity>
</application>

您可以使用 TECH_DISCOVERED 意图过滤器捕获任何(空或非空)标签的事件:

<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>...</activity> 标签中。 <meta-data> 标签必须在 intent-filter 标签之外,但也必须在 activity 标签内。

此外,您需要一个技术筛选器文件 (xml/nfc_tech_filter.xml),它可以正确列出您要筛选的所有标签技术:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <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.NfcBarcode</tech>
    </tech-list>
</resources>

由于 NdefIsoDep 等其他技术在上述基本标记技术(NfcANfcB 等)之上运行,因此无需在过滤器文件中指定它们,因为它们已包含在基本标记技术中。

请注意,如果您在一个 <tech-list> 条目中列出多个 <tech> 条目,它们将使用逻辑 AND 组合,而多个 <tech-list> 条目将使用逻辑 OR 组合。

以上内容将使用您在过滤器文件中列出的任何技术过滤空标签和非空标签。但是,对于非空的 NDEF 格式标签,任何与标签上的数据类型匹配的 NDEF_DISCOVERED intent 过滤器(来自您的应用程序或任何其他应用程序)将优先于 TECH_DISCOVERED intent 过滤器.因此,在这些情况下,您的应用不会收到通知。如果您还想接收此类标签的事件,则需要使用适当的数据类型注册一个 NDEF_DISCOVERED 意图过滤器。例如。如果数据类型是 URL http://www.example.com/

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

最后,尽量避免使用 TAG_DISCOVERED 意图过滤器。这是一种回退,只有在没有其他应用程序为该标签类型注册时才会触发。