DroidGap: how get webpage?

邮差的信 提交于 2020-02-22 08:25:19

问题


My source:

    public class MainActivity extends DroidGap {
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.loadUrl("file:///android_asset/www/index.html");
    }


    @Override
        public boolean dispatchTouchEvent(MotionEvent e) {
            super.dispatchTouchEvent(e);
    // Now what page ??

            return true;
        }

How to get the current web page?


回答1:


Create one external javascript file, write following code in it:

&ltscript>
document.getElementByTagName("body").addEventListener('touchstart',touchPagePressed);// you can use 'onclick' also
function touchPagePressed()
{
   alert(touchPagePressed');
   var sPath=window.location.pathname; 
   var sPage = sPath.substring(sPath.lastIndexOf('/') + 1); 
   alert(sPage); 
   MyAndroid.performClick(sPage);
}
&lt/script>

include that external js file in every html page.

after this, write following code in your onCreate method:

public class MainActivity extends DroidGap {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");

        WebView webView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new Object()
             {
                public void performClick(String currentPageName)
                {
                   // Deal with a click on the body tag
                   System.out.println("PageName- "+currentPageName);  
                }
             },"MyAndroid");
}
}


来源:https://stackoverflow.com/questions/13491274/droidgap-how-get-webpage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!