WebView shouldOverrideUrlLoading Wont Play Video - android

My app displays a webpage using a WebView. I want the user to be able to click on a link to a video, and the video be played in landscape mode. Where I am now, the video doesn't even play??
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mnwv_main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setPluginsEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.meanwhileinwv.com");
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}

Change this:
...
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.meanwhileinwv.com");
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}
with this:
...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
startActivity(in);
return true;
}
else
return false;
}
});
myWebView.loadUrl("http://www.meanwhileinwv.com");
}

Related

How to open external player when using WebView?

How to open external video player when using WebView on JS site? I try to open an external video player through the android application on the webview, but nothing works.
file MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
webView.loadUrl("https://google.com/");
WebViewClient webViewClient = new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
};
webView.setWebViewClient(webViewClient);
}
};
I tried using this code but nothing comes out
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(".avi") || url.contains(".ts") || url.contains(".mkv") || url.contains(".mp4")){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse(url);
intent.setDataAndType( videoUri, "application/x-mpegURL" );
startActivity( intent );
return true;
}else {
return false;
}
}

Open a link in the android browser (WebView)

I recently implemented the shouldInterceptRequest method to detect when the link "http://sitemercado.com.br/valida" clicked to open it in the android browser instead of opening internally in the webview until it worked the link is opened in the browser but when I come back for the webview application, it is also loaded, I would like it to load only in the browser.
My code is the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (findViewById(R.id.pb));
mWebView = findViewById(R.id.webview);
mWebView.setListener(this, this);
mWebView.loadUrl("https://www.sitemercado.com.br/frade");
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Intent i = new Intent(MainActivity.this, off.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
});
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.sitemercado.com.br/valida")) {
return true;
}
String valida = "https://www.sitemercado.com.br/valida";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(valida));
startActivity(i);
Toast.makeText(getApplicationContext(), "1Detectou", Toast.LENGTH_SHORT).show();
return false;
}
});
Where am I going wrong?
Unless I'm mistaken, the following is just wrong:
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.sitemercado.com.br/valida")) {
return true;
}
String valida = "https://www.sitemercado.com.br/valida";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(valida));
startActivity(i);
Toast.makeText(getApplicationContext(), "1Detectou", Toast.LENGTH_SHORT).show();
return false;
}
});
You're getting the host of the URL, then comparing it to a full URL. That's never going to be equal.
Then you launch the link only if that statement is false? That's inverted, since you say in your question that you want it to launch in the browser only if the URL is /valida. The way you have it, no matter what URL you attempt to load, Android will launch a browser pointing to /valida.
Try this instead:
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getPath().contains("valida")) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Toast.makeText(getApplicationContext(), "1Detectou", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});

i have a webview app and facing that when i click on Hyperlink then it open in default browse

I have a webview app and facing a problem that
when I click on Hyperlink then it opens in default browser.
But I want to open that link in same webview.
How to open it in same webview.
Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("file:///android_asset/abc/index.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("file:///android_asset/abc/index.html")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Do it like,
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Return false in all cases.
ok.. you have added a WebViewClient also..
then try it in this way...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
this should work for you..

Telephone link in webview is opening in browser

I have a telephone link on my webview app: tel:062123658 but when I click on it I get webpage not found.
This is my code:
public class FullscreenActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://www.mywebsite.nl");
webView.setVerticalScrollBarEnabled(false);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
How can I fix this?
You should override a function in WebViewClient:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.loadData("Hello World, 317.456.2564 ", "text/html","utf-8");
//webView.loadUrl("http://www.techjini.com/contactus.html");
webView.setVerticalScrollBarEnabled(false);
}
class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
}
Try this way:
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
});

Link in WebChromeClient does not open safari when using WebViewClient shouldOverrideUrlLoading

I use WebChromeClient for opening a link to news.html on my server, it opens safari and shows the content, ok.
But...
When I implent WebViewClient shouldOverrideUrlLoading to intercept the call when it's link to a .pdf file (use another class for it) the WebChromeClient link to news.html stays in webview and does not open safari anymore...
I am doing something wrong, but what?
Code snippet: (links are long so I shortened it)
myWebView = (WebView)findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
//only to catch url override
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("http://www.domain.nl/pdf")){
Intent i = new Intent();
i.putExtra("url", url);
i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true ;
}
else {
return false ;
}
}
});
myWebView.setWebChromeClient(new WebChromeClient());
//loading
myWebView.loadUrl("http://www.domain.nl/news.html");
return false; means that the url wasn't handled and it should be opened in the WebView. If you want to open the url in an external browser you need to explicitly do it.
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("http://www.domain.nl/pdf")){
Intent i = new Intent();
i.putExtra("url", url);
i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true ;
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url));
return true;
}
});
To open the PDF file url :
add the "http://docs.google.com/viewerembedded=true&url=" string at starting of PDF file url string...
try this it work
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView;
webView = new WebView(this);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setPluginsEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
if (webUrl.equals(XmlFileUrl.MemberDownloadAppForm)) {
webUrl = "http://docs.google.com/viewerembedded=true&url=" + webUrl;
}
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
setContentView(webView);
}
An example that helped me to do something similar Here
For geolocation add:
myWebView.getSettings().setGeolocationEnabled(true);
and :
private class MyWebChromeClient extends WebChromeClient {
.........
#Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback)
{
// callback.invoke(String origin, boolean allow, boolean remember);
callback.invoke(origin, true, false);
}
}

Categories

Resources