从不同的Android Studio活动中调用函数结果

huangapple go评论65阅读模式
英文:

Call function result from different activity on android studio

问题

我在网上找到了一个关于创建Android QR码扫描器应用程序的教程。它工作得很好,但扫描的输出是一个吐司通知,就像你在代码中看到的:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (resultCode == Activity.RESULT_OK) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result != null) {
            if (result.contents == null) {
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this, "Scanned: " + result.contents, Toast.LENGTH_LONG).show()
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }
}

我想要做的是让扫描的代码在应用程序内部启动URL。由于我希望页面托管在应用程序内部,我创建了一个带有Web视图的活动,用于加载内部HTML页面。

我使用了扫描器代码,并将积极的结果更改为打开Web视图活动:

startActivity(Intent(this, PagInfoActivity::class.java))

这个方法很好用。每当检测到QR码时,应用程序会自动加载所需的活动。我知道这并不理想,因为QR码链接本身并没有被用来打开页面,但我试图做的是在Web视图加载时使用扫描结果。我创建了包含文本字符串而不是URL的QR码,这样我就可以将它们注入到Web视图的loadURL中,就像这样:

WebView.loadUrl("file:///android_asset/*QR码字符串*.html");

是否可能从MainActivity中调用result.contents呢?

英文:

I found a tutorial online for creating a QR Code Scanner App for Android. It works great but the output of the scan is a toast notification as you can see in the code:

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (resultCode == Activity.RESULT_OK) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result != null) {
            if (result.contents == null) {
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this, "Scanned: " + result.contents, Toast.LENGTH_LONG).show()
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }
  }
 }

What i would like to do is for the scanned code to launch the url inside the app itself. As I want the pages to be hosted in the app itself, what i've done was create an actvity with the webview that loads the internal html page.

I took the scanner code and changed the positive outcome to open the webview activity :
startActivity(Intent(this, PagInfoActivity::class.java))

Which works fine. Anytime a QR Code is detected the app automatically loads the desired activity. I know this is not ideal, as the QR Code link itself is not being used to open the page, but what I was trying to do was to use that scan result on the webview load. I've created QRCodes with text strings instead of URLs so that i could inject them in the loadURL of the webview like this:

WebView.loadUrl("file:///android_asset/*QRCODE string*.html");

Is it possible to call result.contents from the MainActivity?

答案1

得分: 1

有多种方法可以实现这个。第一个选项是“显式意图”
例如,
您需要将其作为附加项传递:

Intent i = new Intent(this, PagInfoActivity.class);
i.putExtra("result", result.contents);
startActivity(i);

然后在您的PagInfoActivity中提取它,就像这样:

Intent intent = getIntent();
String result = intent.getExtras().getString("result");
英文:

There are multiple ways you can achieve this. First option can be Explicit Intents
For Example,
You need to pass it as an extra:

Intent i = new Intent(this, PagInfoActivity.class);
i.putExtra("result", result.contents);
startActivity(i);

Then extract it from your PagInfoActivity like this:

Intent intent = getIntent();
String result= intent.getExtras().getString("result");

huangapple
  • 本文由 发表于 2020年9月26日 19:08:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64076950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定