I'm using mozilla/pdf.js to display a pdf in a webview in my Android application. The framework doesn't need any internet connection to render the pdf. If downloaded the .js file and everything so it can use that. Still every time I want to open a new pdf Android opens the default browser with the wireless settings dialog.
After closing the app and opening it again the PDF is rendered. My internet connection both wireless as data is disabled, so it doesn't need anything of internet, I'm sure about that.
How can I solve this that the webview ask for internet?
This is my code:
webView = (WebView) findViewById(R.id.webViewWindow);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/pdfviewer/index.html");
I also found on the Android dev page that the setAllowFileAccessFromFileURL and the setAllowUniversalAccessfromFileURLS are both not necessary. You should use only the universal one, is this true or do I understand this incorrectly?
I solved this problem by:
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.d("Debug","Error");
}
});
Then you have control about the error and you can like me choose to do nothing.
Related
I've developed a small group of .html pages that are stored in a server.
My android app, using a webview with: setWebChromeClient loads these pages.
minSdkVersion 19
targetSdkVersion 24
The problem:
Everytime I need to load a new page, using a link in my .html page, the new page is opened in an external browser.
Url Overriding
I know about the shouldOverrideUrlLoading() method, when we're using the setWebViewClient().
But unfortunately I can't use the normal webview. I need to use the WebChromeClient() because of some feature that only work with this one. (Like the input file)
My doubt is...
How can I override my URL to force them to load inside of the webChromeClient?
I tried this but with no luck:
webView.setWebChromeClient(new WebChromeClient() {
// (...)
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
You can use following code to achieve this.
WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.
web.setWebChromeClient(new WebChromeClient(){
#Override
public void onReceivedTitle(WebView view, String title) {
getWindow().setTitle(title); //Set Activity tile to page title.
}
});
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
I was using an embedded mobile web application in an Android App making use of the WebChromeClient class and did not want to fiddle around with recompiling the APK.
While looking for the easiest header(); solution (since the mobile web application is in php) I found out that using:
header("Location: url.php", TRUE, 307);
was a quick fix solution without recompiling the Android app.
This allowed the user to re-direct within the app without calling the web browser externally from my app.
Here is the link to the answer by Arindam Nayak where I got the idea from.
Question
I wonder what is the best way to make android webview perform best for the single page application loading.
Situation
I already have a web application with AngularJS.
Currently, I'm building an android application and use webview for the main view of that one.
This android application sends the http request every time when users click the menu on native side.
Sadly, webview loading is very slow since it always get entire resource from my web application, although one of great benefits of the single-page-application is url routing function without refreshing page entirely as I understand correctly. Below is my code for webview loading. Nothing changed I set setCacheMode(WebSettings.LOAD_DEFAULT) though.
webviewFragment.java
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String url = getArguments().getString("URL");
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setAllowFileAccess(true);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
// handle url-scheme
}
webView.setWebChromeClient(new WebChromeClient() {
// handle reaction for UI Component
}
}
I'm developing an app using PhoneGap. So, it's a web app. In this app, I have a requirement to embed a YouTube video and autoplay it whenever the user navigates to that particular page.
I've read that HTML5 video autoplay doesn't work on mobile devices because of bandwidth concers. My question is that is there any way at all to bypass this restriction? I don't mind complex workarounds or hacks that could allow me to do this. Anything at all.
Thanks.
As you pointed out yourself, all autoplay instructions in your code itself will be ignored on load. So we'll implement a function that gets the video in the body and starts playing it.
The following javascript code could do this:
(function() {
document.getElementsByTagName('video')[0].play();
})()
To execute this code after the page has loaded, we need to set a WebViewClient and implement onPageFinished()
webview.setWebViewClient(new CordovaWebViewClient(this, webview) {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
A full example:
webview = new CordovaWebView(this);
setContentView(webview);
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
webview.setWebViewClient(new CordovaWebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
webview.loadUrl("http://html5demos.com/video");
There is a cordova/phonegap plugin that promises to do this. It rely on the Webview method setMediaPlaybackRequiresUserGesture, added in API level 17 (Android 4.2).
It simply call this method on the webview:
WebView view = getWebViewFromPlugin();
view.getSettings().setMediaPlaybackRequiresUserGesture(false);
It's also available in phongap build.
I am experimenting with Android code: I would like to store one value using HTML 5 local storage. For this exercise I' using a page as simple as this one:
http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_webstorage_local_clickcount
My manifest does allow me to hit the internet, and it is min-sdk of 7.
Here is my java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabasePath("");
webSettings.setDomStorageEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://www.xyz.com/test.html");
///xyz.com/test.html is a sample :)
webview.setWebViewClient(new HelloWebViewClient());
}
My problem is that when I close the app, the locally stored value is no longer there. I can browse to the same page using the default browser, and the value is persistent even after closing the emulator, which is exactly the behavior that I am looking for.
This is probably a something extremely simple....any ideas?
It appears the empty string DatabasePath is the problem. I tried similar code and with an empty string path, the value does not persist after the app exits. If I define a specific database path, the value persists as expected.
Try:
webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
If your app use multiple webview you will still have troubles : localStorage is not correctly shared accross all webviews.
If you want to share the same data in multiple webviews the only way is to repair it with a java database and a javascript interface.
This page on github shows how to do this.
hope this help!
Couldn't get it working on all devices (especially with ICS) - even with database path, enabling DOMStorage etc. - using cookies instead helped me out.
I have a requirement where there is a URL = "http://www.example/Open.pdf"
Now from my android application I want to open this PDF file directly in the default PDF viewer.
The moment I click on this link on the webpage, user should be presented with a default PDF viewer opened with this document.
Note: This file should not be stored on the SD card.
How do I proceed for this implementation?
We can open PDF file in the webview without caching it. Write below code in "onCreate" method .
Working code :
String url = "http://www.example.com/abc.pdf";
final String googleDocsUrl = "http://docs.google.com/viewer?url=";
WebView mWebView=new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
mWebView.loadUrl((googleDocsUrl + url));
setContentView(mWebView);
What happens here is you open the PDF using Google Docs. Best Advantage of using above method is the lazy loading of PDF. Does not matter how heavy the PDF is. Google Docs takes care of it.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
There is no way you can open a default PDF view from your application.
If your file is on the server and you want to open it without downloading then this might also pose a greater security concern. If external applications like default adobe reader can access the content on your server, then this is breaking the security framework altogether.
So, best option would be to launch a new instance of browser or webview and show the PDF document in google docs to the user.
This way user can read the document and get back to the recent state of the application as well.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
do you have the others solution besides view pdf file using http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf