can I get the url or request of opening some website as URL is typed or entered in my app. I am implementing parental control. So I just want to block some websites from my app. So can I get the request in my app as user hits any URL in any Browser? Is there any way?
Thanks in advance.
have you tried to add this :
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return true;
}
});
If you are opening your website into a WebView into your android app, you can block it with the shouldOverrideUrlLoading callback:
mView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
(put here your conditions to block or let load an url)
}
}
Related
My WebView converts https to http, as evident from checking the url parameter in shouldOverrideUrlLoading() method.
I have tried using
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE), but it doesn't work.
How do I fix this?
Please help!
you can handle every url as you wish just set up your webview via customized WebViewClient
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startWith("https")) {
//do whatever you need
}
return false;
}else if(....
I have to make a webbrowser for android, so I want to try to block a site.
How can I do that?
Lets say your WebView id is myWebView then what you will do is this :
WebView wb = (WebView) findViewById(R.id.myWebView);
wb.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("http://yourBlockedUrl.com")){
//notify the user that this url is blocked
return true;
}
return false;
}
});
by doing this you are overriding the url loading of your webview you can thus block a url from loading.
say you get the url sent as a string "www.google.com", from the edittext just do a check if this is a blocked url.
for example
if( "www.google.com".equalsIgnoreCase(blocked_string))
{
webview.setVisibility(View.GONE);
warning_view.setVisibilty(View.VISIBLE);
}
or you could try overriding the shouldOverrideUrlLoading() in the WebViewClient class
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(my_url.equals("www.google.com")){
//do something.
}
return true;
}
Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here (via shouldOverrideUrlLoading()).
public void gotoUrl(View view) {
EditText theEditText = (EditText)findViewById(R.id.urlTxt);
theUrl = theEditText.getText().toString();
//
// String blok= "http://www.teknojurnal.com";
webBrowserKu.loadUrl(theUrl);
}
my web browser when klik go, will process this, so what is the problem? why I cant do the steps from anything for blocking one site?
I am passing a url into my application and calling loadUrl on a CordovaWebView. Anytime I try with an external URL it opens the link in an external browser application. I need it to remain in my application.
myCordovaWebView.loadUrl("http://google.com");
myCordovaWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
myCordovaWebView.loadUrl("http://google.com");
I want to add news and weather app in webview. But jumping to anyther website from webview is not an option. How can I restrict a webview for a certain website.
Thanks.
Create your own WebViewClient:
public class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.getHost().equals("weather.com")){
// load link
return false;
}else{
// block link
return true;
}
}
}
Use it like this:
webview.setWebViewClient(new CustomWebViewClient());
EDIT Note:
an example of a getHost() function
If I understand the question correctly you might want to implement your own WebViewClient. That will allow you to handle the page navigation. Check out the link for a tutorial from Google.
viewer.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//Block all URL accesses.
return false;
}
});
That should prevent access to anything. You need to handle specific URL's in this function if you want to allow them.
I have written a simple helloworld app with a WebView which has a link to CNN on a simple.html page in my asset folder.
cnn.com
How can I capture the click on this on my Activity, stop the WebView from navigating, and then inform the Activity that "http://CNN.com" was clicked?
Then you have to set a WebViewClient to your WebView and override shouldOverrideUrlLoading and onLoadResource methods. Let me give you a simple example:
WebView yourWebView; // initialize it as always...
// this is the funny part:
yourWebView.setWebViewClient(yourWebClient);
// somewhere on your code...
WebViewClient yourWebClient = new WebViewClient(){
// you tell the webclient you want to catch when a url is about to load
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
// here you execute an action when the URL you want is about to load
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("http://cnn.com") ){
// do whatever you want
}
}
}