android - 有没有办法将 NDEF 消息传递给字符串?
问题描述
所以我有一个存储 URL 的 NFC 标签,我正在构建一个读取该标签并将应用程序启动到主要活动的 android 应用程序。之后,我有一个显示该网站的 WebView。我的问题是,每个标签都有不同的 url/路径(假设它们是服装,每个都指向网站上的产品)。如何从标签(url)获取 NDEF 消息并将其传递给 webview 的该参数?感谢您的帮助,我将在这里留下我的代码。
我遵循了关于 NFC 的 android 文档,基本和高级,谷歌搜索了每个页面并搜索了 reddit 上的所有内容,但是我找不到任何答案......
这是我的主要活动,它在读取标签后立即打开:
class MainActivity : AppCompatActivity() {
private var adapter: NfcAdapter? = null
// Pending intent for NFC intent foreground dispatch.
// Used to read all NDEF tags while the app is running in the foreground.
private var nfcPendingIntent: PendingIntent? = null
// Optional: filter NDEF tags this app receives through the pending intent.
//private var nfcIntentFilters: Array<IntentFilter>? = null
private val KEY_LOG_TEXT = "logText"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initNfcAdapter()
if (intent != null)
{
processIntent(intent)
}
}
private fun initNfcAdapter() {
val nfcManager = getSystemService(Context.NFC_SERVICE) as NfcManager
adapter = nfcManager.defaultAdapter
}
override fun onResume() {
super.onResume()
enableNfcForegroundDispatch()
}
private fun enableNfcForegroundDispatch() {
try {
val intent = Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
val nfcPendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
adapter?.enableForegroundDispatch(this, nfcPendingIntent, null, null)
} catch (ex: IllegalStateException) {
}
}
override fun onPause() {
disableNfcForegroundDispatch()
super.onPause()
}
private fun disableNfcForegroundDispatch() {
try {
adapter?.disableForegroundDispatch(this)
} catch (ex: IllegalStateException) {
}
}
private fun processIntent(checkIntent: Intent) {
if(checkIntent.action == NfcAdapter.ACTION_NDEF_DISCOVERED) {
val rawMessages = checkIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
}
}
}
这是 webview 活动:
class Webview : AppCompatActivity() {
private lateinit var webview: Webview1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webview)
val myWebView: WebView = findViewById(R.id.webview)
myWebView.webViewClient = object : WebViewClient () {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
if (url != null) {
view?.loadUrl(url)
}
return true
}
}
myWebView.loadUrl("https://website.com")
myWebView.settings.javaScriptEnabled=true
myWebView.settings.allowContentAccess=true
myWebView.settings.domStorageEnabled=true
myWebView.settings.useWideViewPort=true
myWebView.settings.setAppCacheEnabled(true)
}
}
我想将 NDEF 消息传递给 myWebVIew.loadUrl()。
解决方案
推荐阅读
- azure-log-analytics - Azure Log Analytics - 无法添加数据源
- asciidoctor - Asciidoctor 不包括章节计数中的第一个标题
- javascript - JS将一个变量从嵌套函数返回到全局范围
- android - 新引入的 ViewBinding 不起作用。知道为什么吗?
- css - 在 Angular 中更改工具提示背景颜色
- java - 如何自定义 spring initializr 以提供与 start spring 相同的功能?
- mysql - 运行 ALTER TABLE 时出现 Mysql FATAL 错误
- java - 为什么我在使用 JSON 的 Hitting API 中出现错误
- kubernetes - “CrashLoopBackOff”错误消失,但新错误“Connection Refuesd”
- python - 用于响应本机移动应用程序的自动化工具