首页 > 解决方案 > 有没有办法将 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()。

标签: androidkotlinwebviewnfc

解决方案


推荐阅读