gpt4 book ai didi

java - 无法从 webview android 中从 Javascript 调用的 Java 方法获取数据

转载 作者:行者123 更新时间:2023-11-30 03:00:40 26 4
gpt4 key购买 nike

我正在开发一个应用程序,当我单击 web View 中的链接并期望该方法的结果时,我会在其中执行 Java 方法。在那个方法中,我用 startActivityForResult 开始了另一个 Activity ,它工作得很好(我在那个 Activity 上放了一些日志,一切都很好),但看起来这个方法没有返回任何东西..

这是我的代码:

HTML : (所有包含都可以)

<script type="text/javascript">
$("#run-qrscan").on("click", function(){

var qr_data = window.WebAppInterface.startScan();
// startScan is the Java method I call
// call the function opens the activity etc.. but doesn't get any result

$("#display-qrresult").html(qr_data); // doesn't display anything
});
</script>

我的 Activity :WebActivity.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);

WebView webview = (WebView) findViewById(R.id.aw_webView);

WebAppInterface jsInterface = new WebAppInterface(this);
webview.getSettings().setJavaScriptEnabled(true);

webview.setWebChromeClient(new WebChromeClient());

webview.addJavascriptInterface(jsInterface, "WebAppInterface");
webview.loadUrl("file:///android_asset/index.html");
}

我的 Javascript 界面:WebAppInterface.java

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.webkit.JavascriptInterface;

public class WebAppInterface {

private static Activity activity;
public static int WEB_APP = 0;

private static int REQUEST_FOR_ACTIVITY_CODE_SCAN = 1000;
private static String qr_data = "";
private static Intent intent;

public WebAppInterface(Activity activity) {
WebAppInterface.activity = activity;
}

@JavascriptInterface
public static String startScan() {
Intent intent = new Intent(activity, ScanActivity.class);
WEB_APP = 1;

activity.startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE_SCAN);

//Wait for startActivityForResult to end -since it's asynchronous
while(WEB_APP == 1) {}

Log.d("qr_return", qr_data); // Doesn't display it.

return qr_data; // Empty
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Scan QR
if (requestCode == REQUEST_FOR_ACTIVITY_CODE_SCAN) {
if (resultCode == Activity.RESULT_OK) {
qr_data = data.getStringExtra("qr_data");
}
}
WEB_APP = 0;
}
}

那我做错了什么?谢谢。

最佳答案

抱歉,我的错,我没有注意到我调用 startActivityForResult 的类只是一个类而不是 Activity.. 所以当返回数据时它不是回到它..

我发布这段代码以防像我这样的人没有注意到这一点。

1- 删除 WebAppInterface.java 类

2- WebActivity.java :

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class WebActivity extends Activity {

public static int WEB_APP = 0;

private static int REQUEST_FOR_ACTIVITY_CODE_SCAN = 1000;
private static String qr_data = "";

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);

WebView webview = (WebView) findViewById(R.id.aw_webView);

WebAppInterface jsInterface = new WebAppInterface();
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.addJavascriptInterface(jsInterface, "WebAppInterface");
webview.loadUrl("file:///android_asset/index.html");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Scan QR
if (requestCode == REQUEST_FOR_ACTIVITY_CODE_SCAN) {
if (resultCode == Activity.RESULT_OK) {
qr_data = data.getStringExtra("qr_data");

Log.d("QR", qr_data);
}
}
WEB_APP = 0;
}

public class WebAppInterface {

WebAppInterface() {}

@JavascriptInterface
public String startScan() {
Intent intent = new Intent(WebActivity.this, ScanActivity.class);
WEB_APP = 1;

startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE_SCAN);

while(WEB_APP == 1) {}

Log.d("qr_return", qr_data);

return qr_data;
}
}
}

关于java - 无法从 webview android 中从 Javascript 调用的 Java 方法获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22594688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com