I am running a WebView in an Android app and need to force either the platform parameters or flags or whatever to be "Mobile" instead of the default "Web". In other words I need the requests (calls) made by the WebView to indicate the platform is for "mobile" or "in-app" and not a "Web" request.
I've found no documentation on how to do this or anything like it. The Android Developer documentation does not offer enough under-the-hood info on how to do the above.
You can set custom webview client and trace all the urls.
WebViewClient myclient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Trace the Url here
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Trace the Url here
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// Trace the Url here
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Trace the Url here
}
};
mWebView.setWebViewClient(myclient);
Vote Up if this helps.
Related
I have written custom webviewclient class to override onPageStarted, onPageFinished etc in cordova 3.7 which was working fine.
In following code is I have hosted the www directory to web server and interacting cordova plugins from there (barcodescanner, nfc, bluetooth etc).
public class MainActivity extends CordovaActivity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
loadUrl("https://example.com");
}
public class CustomCordovaWebViewClient extends CordovaWebViewClient {
public CustomCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
super(cordova, view);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "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);
}
}
}
After a year, I have migrated project from cordova 3.7 to cordova 6 but I found above code broken like CordovaWebViewClient, super.onPageStarted etc can't resolve symbols. I also tried CordovaWebViewImpl and confused myself.
After searching alot on google I found solution which were mostly given in 2011-14 which are not applicable. I couldn't found cordova docs helpful.
It was replaced by SystemWebViewClient
You should do something like this:
SystemWebView wv = (SystemWebView)appView.getView();
wv.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine)appView.getEngine()){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "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);
}
});
Cordova 4 removed the CordovaWebViewClient : look here
You may use WebViewClient instead of CordovaWebViewClient (The cordova-plugin-inappbrowser plugin use that for override onPageStarted event).
public class CustomCordovaWebViewClient extends WebViewClient
Apache cordova removed CordovaWebViewClient inorder to support external webviews like Crosswalk. If you check out the 14 changed files link in the following commit link, you could see CordovaWebViewClient is removed and AndroidWebViewClient is added.
So i guess you cannot use the same old code work in Cordova 6.0
You can probably try using org.apache.cordova.engine.SystemWebViewClient instead.
Infact, the same question is answered here and it was also accepted. So i believe this is the possible solution to the issue. Hope it helps.
I have written custom webviewclient class to override onPageStarted, onPageFinished etc in cordova 3.7 which was working fine.
In following code is I have hosted the www directory to web server and interacting cordova plugins from there (barcodescanner, nfc, bluetooth etc).
public class MainActivity extends CordovaActivity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
loadUrl("https://example.com");
}
public class CustomCordovaWebViewClient extends CordovaWebViewClient {
public CustomCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
super(cordova, view);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "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);
}
}
}
After a year, I have migrated project from cordova 3.7 to cordova 6 but I found above code broken like CordovaWebViewClient, super.onPageStarted etc can't resolve symbols. I also tried CordovaWebViewImpl and confused myself.
After searching alot on google I found solution which were mostly given in 2011-14 which are not applicable. I couldn't found cordova docs helpful.
It was replaced by SystemWebViewClient
You should do something like this:
SystemWebView wv = (SystemWebView)appView.getView();
wv.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine)appView.getEngine()){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "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);
}
});
Cordova 4 removed the CordovaWebViewClient : look here
You may use WebViewClient instead of CordovaWebViewClient (The cordova-plugin-inappbrowser plugin use that for override onPageStarted event).
public class CustomCordovaWebViewClient extends WebViewClient
Apache cordova removed CordovaWebViewClient inorder to support external webviews like Crosswalk. If you check out the 14 changed files link in the following commit link, you could see CordovaWebViewClient is removed and AndroidWebViewClient is added.
So i guess you cannot use the same old code work in Cordova 6.0
You can probably try using org.apache.cordova.engine.SystemWebViewClient instead.
Infact, the same question is answered here and it was also accepted. So i believe this is the possible solution to the issue. Hope it helps.
Is it possible to restrict WebViews so that when you have let's say one webview for website but you just want the view to stick to that page and only that page?
Great example is I have a twitter WebView that is following a tag. I just want the user to be able to follow that tag not be able to go deeper into twitter stuff.
It's already a pre-set link,
Button--> opens web view with set link to twitter.
So has to be some kind of check for on press or something like I think.
something like this should do it.
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("somedomain.com")) {
view.loadUrl(url);
}
return true;
}
});
In-fact the above is a good solution but if you want an elaborate one. Try this one. I don't know whether this is proper solution for your query. play around with this.
webview.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!url.equals("your_domain_name.com/")){
setResult(RESULT_OK);
finish();
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(!url.equals("your_domain_name.com/")){
finish();
}
}
#Override
public void onTooManyRedirects(WebView view, Message cancelMsg,
Message continueMsg) {
super.onTooManyRedirects(view, cancelMsg, continueMsg);
cancelMsg.sendToTarget();
}
}
I want to trigger a function after webview's finished loading a webpage and received no error. According to my experiment, the onPageFinished() will be triggered several times and in most of the cases, it will be executed before onReceivedError().
So how can I know that the webpage is loaded and no error is received?
You can set the WebViewClient in the WebView and Override its Callback methods which will let you know when the webpage has finished loading the page. For example
webView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
if(progess!=null){
progess.dismiss();
}
progess = ProgressDialog.show(LoadUrl.this, "", "Loading please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (Uri.parse(url).getHost().equals("callback")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view, url);
// Add YOUR CODE HERE
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(LoadUrl.this, "Sorry! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();
}
}
Here onPageFinished(WebView view, String url) will be called when webView has finished loading the current Page.
Hope it helps :)
I'm looking to develop an application to track how long a site takes to load on the default android browser. Does the browser have any method of tracking when the site is fully loaded?
on page started and on page finished are the methods to
get the the start time and end time of loading of the webview
WebView search_webview;
search_webview = (WebView) findViewById(R.id.show_webview);
search_webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
_dialog.dismiss();
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try{
_dialog.dismiss();
Toast.makeText(ShowWebView.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
}catch (Exception e) {
// TODO: handle exception
}
}
});