如何使用url为读取nfc标记指定mimeType

Vít*_*ega 2 android nfc mime-types

我有一个Uri NFC标签: pay://myName/12345

在我的意图过滤器中我有:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <data android:mimeType="text/plain" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

我如何指定MIME类型的数据以启动我的应用程序没有应用程序选择器?

Com*_*are 11

好吧,理想情况下,你不会发明像这样的非标准方案.最好使用指向网页的HTTP URL创建标记,其中Android活动也会处理该URL - Android活动<intent-filter>将更具体,因此在安装时优先.这样,标签适用于更多设备.

话虽如此,无论URL语法如何,都要使用适当的<data>元素.而不是使用android:mimeType,使用android:scheme和其他属性.

这是一个示例项目,允许用户将URL写入符合NDEF的标记(通过浏览器中的"共享选项"菜单项共享).如果您为标记编写特定URL,则应用程序还会有一个活动,该活动将在扫描标记时启动,由此清单条目提供:

    <activity android:name="URLHandler"
              android:label="@string/app_name">
        <intent-filter android:label="@string/app_name">
          <action android:name="android.nfc.action.NDEF_DISCOVERED" />
          <data android:scheme="http"
                android:host="commonsware.com"
                android:path="/nfctest"
          />
          <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)