Android: Callbacks with WebView component? - android

I am doing authentication with a third-party site that's supposed to redirect back to my app with auth token (OAUTH).
I have the callback working properly if I open the 3rd party site in a separate browser process via
this.startActivity(new Intent(Intent.ACTION_VIEW, uri));
but, if I embed a WebView component in my layout, and open the url in that, the callback does not work. Webview says "You do not have permission to open myapp://callback?token=...." and quickly refreshes to "Web page not available...temporarily down...blah blah"
Any ideas?

You need to implement a WebViewClient to intercept the custom URI before it is loaded. The Hello, WebView tutorial shows a simple example. Where they have:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
You can change "view.loadUrl(url)" to check if the URL is your custom URL and handle it however you want.

Related

Android WebView URL problems

I have android app hosting a web site in WebView. It works fine, but has one issue. Whenever users search in Google and click on page of this site, the application is opening not at that page but on home page of the web site.
For example: they click on www.example.com/blog - WebView app is open but it shows homepage, not blog.
Links (navigation) works fine inside of application when it starts.
try these code
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return false;
}
});
when you click on any link on webview then view.loadurl() method again load new url.

can we use browser other than chrome to load web activity in android

In my app I am trying to open web application which needs flash player but as I am new to android I only know opening web view with chrome client. is there any method to open webview with any other browser instead of chrome.
I am planing to open that web activity in puffin browser as it supports flash player
Write this to open URL in your own webview :
web.setWebViewClient(new myWebClient());
To enable Javascript :
web.getSettings().setJavaScriptEnabled(true);
When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView.
EDIT
Add this to your code and see if its work:
Make sure shouldOverrideUrlLoading(...) should return false to prevent the default browser from being opened.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}

How to set webviewClient for webView dynamically?

For instance, there is a WebView component in my fragment. I use it to load all webpages by the different urls, sort of stupid but efficient. As you know, we specify our webviewclient (like WvjbWebViewClient or jsBridge) to handle all requests. Now that we want to load third-platform websites with our WebView sometimes, however, we don't want to supply our business function for them, not for anything else, but for our safety of communication.
Our company domain is xxx.com. What I want to do is: when the webview loads those webpages on this domain, use our customer WebViewClient, otherwise use a simple WebViewClient(new a instance). How to resolve it? (Should we consider url redirection?)
WebViewClient allows you to upload any specified URL, selected in the WebView, in WebView itself, and do not run the browser. For this functionality meets shouldOverrideUrlLoading(WebView, String) method. If it returns true - we do not need to launch a third-party browser, and upload their own content here.
Here is an example where we choose if we can open the content in our app, or we need to open browser:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("xxx.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
This can help if think.

How do I open another app in my WebView app?

I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.

How to display Web site in Android WebView

I am new to Android WebView, in my application i need to display a web site conatins 3 web pages. In the first web page there will be a link to navigate to the second page and second to third page. I given the URL in the WebView, the first page is displayed perfectly, when i click the link it directly opens the browser application to display the second page. But i want to display the second page in the WebView itself. Please find my code below:
WebView forumView=(WebView)findViewById(R.id.forumView);
forumView.getSettings().setJavaScriptEnabled(true);
forumView.loadUrl("url");
As i said i am very new to WebView my code might be wrong, please help me to solve this problem.
Thanks in Advance,
Rajapandian
This piece of code will help you.
wbb = (WebView) findViewById(R.id.webView_tobe_loaded);
WebSettings wbset=wbb.getSettings();
wbset.setJavaScriptEnabled(true);
wbb.setWebViewClient(new MyWebViewClient());
String url="http://www.google.com";
System.out.println(getdeviceid());
wbb.getSettings().setJavaScriptEnabled(true);
wbb.loadUrl(url);
You'll have to intercept the clicks yourself if you don't want the default Android behavior.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Categories

Resources