SE 上有大量关于如何执行此操作的示例,但我似乎无法让它工作。我想要做的是当扫描带有 dev.example.com 的标签时,我的应用程序已启动,如果已安装,或者如果未安装,只需转到标签中的 URL。无论应用程序是否正在运行,这都应该发生。
在 list 中:
<activity android:name=".MainActivity">
<intent-filter android:label="@string/app_name">
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="dev.example.com"
android:scheme="https" />
</intent-filter>
我有一个带有 https://dev.example.com 的 NFC 标签写入第一个 NDEF 记录。
无论我做什么,如果我用我的 S7 扫描它,它都会弹出
NFC Request
You are being requested to open a web address tag (https://dev.example.com)
[] Dont ask me about nfc again
cancel ok
(我从不点击“不要再问我”) 单击“确定”启动默认浏览器。
如果我更改 list 以过滤纯文本 NDEF..
<intent-filter android:label="@string/app_name">
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
然后我写了一个文本 NDEF 记录,我的应用确实启动了。
为什么 URL 不起作用?
我也试过添加
android:pathPrefix="/app"
到 url Intent 并写入 https://dev.example.com/app到标签。我的应用仍然没有拦截任何 URL NDEF 记录。
我还有一个
protected void onNewIntent(Intent intent) {}
在我的主要 Activity 中,但它从未被调用,即使文本 NDEF 启动应用程序也是如此。但一次一个问题..
我真的没有任何其他手机可以试用。我在另一台 S7 上试过,它只是启动了 URL,无论是否安装了该应用程序。 (他可能勾选了“不要再问”)
谢谢。
最佳答案
为了避免弹出窗口,您应该将 pathPrefix
属性添加到 URI Intent 中:
<data
android:host="dev.example.com"
android:scheme="https"
android:pathPrefix="/" />
关于android - 无法过滤 URL NDEF Android Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49115009/