问题
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:
<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>
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