JavascriptInterface不适用于Android Studio

时间:2018-03-20 09:54:11

标签: java android android-studio android-webview

我正在尝试使用Android Studio创建并将javasacriptinterface绑定到Android应用程序。我想这样做,以便我可以从我的JS页面发送命令到我的Android应用程序。我尝试了以下代码,但是当我尝试操作JS函数时,似乎没有任何事情发生。

browser.java

public class browser extends AppCompatActivity {
    Runnable timeoutClose = new Runnable() {
        @Override
        public void run() {
            finish();
        }
    };

    public static Handler myHandler = new Handler();
    private static final int timeout = 600000;
    public String storeCurrentURL = "";
    public boolean newSiteFound;
    private WebView mWebView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);

        mWebView.setWebViewClient(new WebViewClient());
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setAppCacheEnabled(false);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setEnableSmoothTransition(true);
        CookieManager.getInstance().setAcceptCookie(true);
        mWebView.clearCache(true);

        mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

        mWebView.loadUrl("http://localhost:8080/esv/esvvr.html");

        new createConnection().execute("http://localhost:8080/esv/StoreURL.txt");
    }

    @Override
    public void onResume () {
        super.onResume();
        new createConnection().execute("http://localhost:8080/esv/StoreURL.txt");
    }


    public class createConnection extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            BufferedReader br = null;
            HttpURLConnection con = null;

            try {
                URL url = new URL(params[0]);

                con = (HttpURLConnection)url.openConnection();

                con.connect();

                InputStream is = con.getInputStream();

                br = new BufferedReader(new InputStreamReader(is));

                StringBuffer buffer = new StringBuffer();

                String line;
                String link = null;

                while((line = br.readLine()) != null) {
                    buffer.append(line);
                }

                br.close();

                if (buffer.toString() != storeCurrentURL) {
                    storeCurrentURL = buffer.toString();
                    newSiteFound = true;
                    return buffer.toString();
                } else {
                    newSiteFound = false;
                }


            } catch (MalformedURLException e) {
                System.out.println();
            } catch (IOException e) {
                System.out.print("IOException");
            } finally {
                if(con != null) {
                    con.disconnect();
                }
                try {
                    if (br != null) {
                        br.close();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String urlInFile) {
            if (newSiteFound && urlInFile != null) {
                super.onPostExecute(urlInFile);
                mWebView.loadUrl(urlInFile);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_browser, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return;
        }
        super.onBackPressed();
    }

    public void restartTimeout() {
        myHandler.removeCallbacks(timeoutClose);
        myHandler.postDelayed(timeoutClose, timeout);
    }

    @Override
    public void onUserInteraction() {
        restartTimeout();
    }


}

WebAppInterface.java

public class WebAppInterface {

    private Context context;

    public WebAppInterface(Context context) {
        this.context = context;
    }

    @JavascriptInterface
    public void showToast(String message) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

}

感谢任何帮助, 感谢

0 个答案:

没有答案