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

时间:2014-03-23 17:52:41

标签: java javascript android webview

我在App上工作,当我点击webview中的链接并期望该方法的结果时,我执行Java方法。在那个方法中,我用startActivityForResult开始了另一个活动,它运行得很好(我已经在那个活动上放了一些日志,而且每个事情都很好),但看起来这个方法并没有返回任何东西..

这是我的代码:

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>

我的活动: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;
    }
}

那么我做错了什么?谢谢。

1 个答案:

答案 0 :(得分:1)

抱歉,我的不好,我没注意我调用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;
        }
    }
}