在Javascript接口类中加载URL

时间:2015-04-05 12:28:33

标签: android

我在界面中加载了一个网址,但它们无法正常工作;

@SuppressLint({ "SetJavaScriptEnabled", "NewApi" }) public class MainActivity extends ActionBarActivity {
     WebView Browser;
     String website ="http://192.168.1.4/Soft/Bigcats/Ba/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          Browser=(WebView) findViewById(R.id.webView1) ; 
       class MyJavaScriptInterface
       {

           @JavascriptInterface
           @SuppressWarnings("unused")
          public void Danyial(String Data)
           {
                Browser.loadUrl("http://enjoybaba.com");
             }
       }
       Browser.addJavascriptInterface(new MyJavaScriptInterface(), "api");
       Browser.loadUrl(website); 
       Browser.setWebViewClient(new WebViewClient());

    }

}

1 个答案:

答案 0 :(得分:1)

您可以通过在Javascript接口类上添加这些来实现此目的

private Activity activity;
        public JavaScriptInterface(Activity act) {
            this.activity = act;
        }

然后在UI线程上运行loadUrl

 activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myWebView.loadUrl("http://google.com");
                    }
                });