简体   繁体   English

Android中的Webview只能在4.2+版本中使用,而不能在2.3.3中使用

[英]Webview in Android which works in 4.2+ and not in 2.3.3

Application which works in Android 4.2.2 using webview javascriptInterface which is not working in Android 2.3.3. 使用webview javascriptInterface在Android 4.2.2中工作的应用程序在Android 2.3.3中不工作

It has targetSdkVersion="19" 它具有targetSdkVersion =“ 19”

and Build property as 4.2 和Build属性为4.2

I have created emulator with 2.3.3 and Device 3.7" WVGA. 我已经使用2.3.3和设备3.7“ WVGA创建了模拟器。

OnCreate Here - 在这里创建-

@JavascriptInterface
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    vWebview = (WebView) findViewById(R.id.vWebview);
    btnView = (Button) findViewById(R.id.btnView);
    txtView = (TextView) findViewById(R.id.txtView);

    vWebview.setWebChromeClient(new WebChromeClient());
    vWebview.setWebViewClient(new WebViewClient());

    try {
        if ("2.3".equals(Build.VERSION.RELEASE)) {
         javascriptInterfaceBroken = true;
        }
        } catch (Exception e) {
        // Ignore, and assume user javascript interface is working correctly.
        }

        // Add javascript interface only if it's not broken
        if (!javascriptInterfaceBroken) {
            vWebview.addJavascriptInterface(new JScriptInterface(),
                    "AndroidFunction");
        }



    WebSettings websetting = vWebview.getSettings();
    websetting.setJavaScriptEnabled(true);
    Log.d("Main", "Called");
    vWebview.loadUrl("file:///android_asset/sample.html");
    btnView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //vWebview.loadUrl("javascript:showAndroidToast('Hello')");
            try{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    vWebview.loadUrl("javascript:showAndroidToast('Hello')");
                }
            });
        }
        catch(Exception ee){
            Log.d("Exception", "Called" + ee.toString());

        }
        }
    });

}

JsInterface - JsInterface-

public class JScriptInterface {

    public JScriptInterface() {
        // TODO Auto-generated constructor stub
    }



    public void showToast(String toast) {
        try{
        Log.d("JS", "Called" + toast);
        // txtView.setText(toast);
        Toast.makeText(MainActivity.this, toast, Toast.LENGTH_LONG).show();
        }
        catch(Exception ee){
            Toast.makeText(MainActivity.this, ee.toString(), Toast.LENGTH_LONG).show(); 

        }
    }



}

I think you might have bumped into this known issue . 我认为您可能遇到了这个已知问题 And here's how to work around it: Handling Android 2.3 WebView's broken AddJavascriptInterface 解决方法如下: 处理Android 2.3 WebView损坏的AddJavascriptInterface

Credits to the original poster here 归功于原始海报在这里

In the future, consider describing your issues in more details. 将来,请考虑更详细地描述您的问题。 "which is not working" does not give any idea of the problem, so I just googled "javascriptinterface 2.3.3" -- then again, you could have done it yourself. “不起作用”没有给出问题的任何想法,所以我只用谷歌搜索“ javascriptinterface 2.3.3”-再次,您可以自己完成。

UPD You need to put the methods from the workaround as overrides to your WebViewClient . UPD您需要将替代方法中的方法作为对WebViewClient替代。 For your code, it should look something like this: 对于您的代码,它应该看起来像这样:

vWebview.setWebChromeClient(new WebChromeClient());

try {
   if ("2.3".equals(Build.VERSION.RELEASE)) {
      javascriptInterfaceBroken = true;
   }
} catch (Exception e) {
   // Ignore, and assume user javascript interface is working correctly.
}
// Add javascript interface only if it's not broken
if (!javascriptInterfaceBroken) {
    vWebview.addJavascriptInterface(new JScriptInterface(),
            "AndroidFunction");
}

if (!javaScriptInterfaceBroken) vWebview.setWebViewClient(new WebViewClient());
else {
    vWebview.setWebViewClient(new WebViewClient() {
        // And here you put the code from the article
        @Override
        public void onPageFinished(WebView view, String url) { ... }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) { ... }
    });
}

Kindly check in devices instead of Emulator. 请检入设备而不是模拟器。 When i checked in Android 2.3.6 its worked well. 当我在Android 2.3.6中签入时,它运行良好。 Unfortunately i didn't get any 2.3.3 device which same as our emulator. 不幸的是,我没有得到任何与我们的模拟器相同的2.3.3设备。 If anyone have the device, Kindly check and Confirm me. 如果有人拥有该设备,请检查并确认我。 Thanks a lot for the valuable comments. 非常感谢您的宝贵意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM