DroidGap:如何获取网页?

时间:2012-11-21 10:31:01

标签: android cordova

我的来源:

    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;
        }

如何获取当前网页?

1 个答案:

答案 0 :(得分:2)

创建一个外部javascript文件,在其中编写以下代码:

<script>
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);
}
</script>

在每个html页面中包含该外部js文件。

之后,在onCreate方法中编写以下代码:

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");
}
}