I work with Android and Phonegap, and currently I am having problems with one simple task. I need to configure webViewClient in the PhoneGap web browser to lock the page URL and work with it.
This is the code:
public class PhoneGapTest extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setBooleanProperty("loadInWebView", true); super.clearCache(); super.keepRunning = false; super.loadUrl("file:///android_asset/www/index.html"); super.appView.setWebViewClient(new WebViewClient(){ @Override public void onPageStarted(WebView view, String url, Bitmap bitmap) { Log.i("TEST", "onPageStarted: " + url); } @Override public void onPageFinished(WebView view, String url) { Log.i("TEST", "onPageFinished: " + url); } }); }
This code does not work, the page never loads, and I get a TIMEOUT ERROR, but if I delete the "setWebViewClient" part, the page will load perfectly.
I saw that there is a CordovaWebViewClient class, do I need to use this instead of WebViewClient? I found this on the Internet:
this.appView.setWebViewClient(new CordovaWebViewClient(this){ @Override public boolean shouldOverrideUrlLoading(final WebView view, String url) { Log.i("BugTest", "shouldOverrideUrlLoading: " + url); return true; } @Override public void onPageStarted(WebView view, String url, Bitmap bitmap) { Log.i("TEST", "onPageStarted: " + url); } @Override public void onPageFinished(WebView view, String url) { Log.i("TEST", "onPageFinished: " + url); } @Override public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){ } });
But this code doesn't work either, I still have ERROR TIMEOUT. I also saw that there is already a webVieClient element, but I do not want to use it and how.
I work with Phonegap version 1.9.0
thank you for reading
Answer Simon:
It worked, thanks!
public class MainActivity extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.init(); super.appView.clearCache(true); super.appView.clearHistory(); this.appView.setWebViewClient(new CustomCordovaWebViewClient(this)); super.loadUrl("file:///android_asset/www/index.html"); } public class CustomCordovaWebViewClient extends CordovaWebViewClient { public CustomCordovaWebViewClient(DroidGap ctx) { super(ctx); } @Override public void onPageStarted(WebView view, String url, Bitmap bitmap) { super.onPageStarted(view, url, bitmap); Log.i("TEST", "onPageStarted: " + url); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); Log.i("TEST", "onPageFinished: " + url); } @Override public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){ super.doUpdateVisitedHistory(view, url, isReload); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); } } }