I am using a WebView in order to play YouTube vidoes in my Android app.
It worked fine, but I seem to have a problem with Jelly Bean (and maybe later versions, I don't know yet).
The first time I play a video it works fine, but when I try to play it again or even another video, after closing the WebView and re-open it, I see the video loading but then enters into pause mode (as can be seen in the image attched).
When closing the WebView I'm using this code, which I think is the cause of the problem:
Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webview, (Object[]) null);
Did someone encounter this problem?
Left image - the video is played normally.
Middle image - trying to load it again.
Right image - video is paused and never started.
This is the code I'm using:
private void loadUrl()
{
progress.setVisibility(View.VISIBLE);
webview.setWebChromeClient(new WebChromeClient());
if (Build.VERSION.SDK_INT < 8) {
webview.getSettings().setPluginsEnabled(true);
} else {
webview.getSettings().setPluginState(PluginState.ON);
}
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url)
{
progress.setVisibility(View.VISIBLE);
webview.loadUrl(url);
isLoading = true;
return true;
}
#Override
public void onPageStarted (WebView view, String url, android.graphics.Bitmap favicon)
{
progress.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError (WebView view, int errorCode, String description, String failingUrl)
{
progress.setVisibility(View.GONE);
isLoading = false;
}
#Override
public void onPageFinished (WebView view, String url)
{
progress.setVisibility(View.GONE);
isLoading = false;
}
});
webview.loadUrl(url);
}
I solved the problem by clearing/destroying the webview:
if (webview != null)
{
if (isLoading)
{
webview.stopLoading();
}
webview.clearCache(true);
webview.clearView();
webview.freeMemory();
webview.destroy();
}
Related
got a webview loading a page on finished the javascript function getting called
but when i try to browser.loadUrl to new page this not working
how can i change the current page of webview after this loaded?
code:
browser = (WebView) findViewById(R.id.w1);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.addJavascriptInterface(this, "HTMLOUT");
browser.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
browser.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
});
browser.loadUrl("page1");
#JavascriptInterface
public void processHTML(String html) {
if (html == null)
return;
browser.loadUrl("page2"); <----
here are some suggestions, hope it helps
You should use view.loadUrl in onPageFinished
To avoid infinity reload page, you should check url in onPageFinished
Following is a rough sample (without loading javascript), sometimes onPageFinished may need time to load, just wait for a second
WebView browser = new WebView(MainActivity.this);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(!url.contains("google")){
Log.i("test", "onPageFinished, reload another");
view.loadUrl("https://www.google.com");
}
}
});
browser.loadUrl("https://www.yahoo.com");
I need to load a PDF file within my webview on a button click within it. I have used google docs URL for loading the same. It works perfectly in S8 edge(v9.0) but it's not working in S6 edge(v6.0.1). It is not calling the shouldOverrideUrlLoading method even. Not able to understand what is the exact reason for it.
My code:
private final String googleDocs = "http://docs.google.com/gview?embedded=true&url=";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString().endsWith("pdf")) {
String pdfUrl = googleDocs + request.getUrl().toString();
view.loadUrl(pdfUrl);
} else {
view.loadUrl(request.getUrl().toString());
}
return true;
}
});
WebSettings webSettings = binding.webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
I am using a WebView that loads a page with some images in it..
I need to detect exactly when it finishes loading and rendering all images in the page.
I tried to use "onPageFinished" but it gets called before all images are completely downloaded and visible.
Same thing goes for "onProgressChanged"..
Is there a method where I can know when exactly does my WebView completely load all its views and images and they are visible on the screen??
In my working code I am using next variant:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
// try to use this pieсe of code
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// put here your code
System.out.println("All images are downloaded and displayed");
}
});
webView.addJavascriptInterface(this, "android");
webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
if you have several redirects it may fail. This approach solves most problems though
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(
WebView view, WebResourceRequest request) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
//HIDE LOADING IT HAS FINISHED
} else {
redirect = false;
}
}
});
Im trying to load a URL using WebView, but the web view does not seem stable - sometimes the page loads 100%, sometimes it stops loading on 30%/50%/75% (random).
I have the WebView being created inside an Android Bolts task, and the WebView is not being showed for the user, it only loads some javascript for future requests.
Code:
private Task<Boolean> task_SetupJavascript(){
final Task<Boolean>.TaskCompletionSource myTask = Task.create();
WebView webview = new WebView(getActivity());
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: LocalStorage.set('valueA', 'valueB', 'valueC');");
myTask.setResult(Boolean.TRUE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// never fired during tests
Log.e(TAG,"onReceivedError ---> Something went wrong... ");
myTask.setCancelled();
}
});
webview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.e(TAG, ""+newProgress);
}
});
webview.loadUrl(URL);
webview.onResume();
return myTask.getTask();
}
I can't figure out what can be causing the WebView to stop loading randomly in the middle of the progress.
Sometimes it fails the first time it runs this task, sometimes it fails after the 3/4/5 time.
I have a simple webview ,nothing complex
URL = getIntent().getExtras().getString(FinalVariables.LOAD_URL);
mWebView =(WebView) findViewById(R.id.mywebview);
progressBar = (ProgressBar)findViewById(R.id.progressbar);
if(!TextUtils.isEmpty(URL))
mWebView.loadUrl(URL);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
But for some reason if the opened page has a video in it, it won't get played
this is what I am seeing now.But when I run this same code on sony device,no problems there.I tried enabling the android:hardwareAccelerated="true" in AndroidManifest.xml but didn't work.Any help is appreciated.
I found a solution for this and was quite simple.Adding a single line on shouldOverrideUrlLoading fixed the problem ie,
mWebView.setWebChromeClient(new WebChromeClient());
Hope this will help someone.