Webview is not opening the new intent - java

I'm trying to create a application that uses webview in order to load a page. In this page i have some link that i need to open them with another intent with an application called MX Player for video playing. But the problem is that my webview even if i click in a link that doesn't ends with mp4 or a link that ends with mp4 it does nothing. Here is my code :
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setAllowFileAccess(true);
webView.loadUrl("LINK OF MY PAGE");
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
url = webView.getUrl();
if (url.endsWith(".m3u8") || url.endsWith(".ts") || url.endsWith(".amv") || url.endsWith(".mp4"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse(url);
intent.setDataAndType( videoUri, "application/x-mpegURL" );
intent.setPackage( "com.mxtech.videoplayer.ad" );
startActivity( intent );
}
webView.loadUrl(url);
return true;
}
});
}
}

According to the documentation shouldOverrideUrlLoading (WebView view, String url) should return
true if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
This means that your overriden shouldOverrideUrlLoading should return true if you want to handle the URL by yourself, and false if you don't want to handle the URL, and WebView should open it. So your code should look like this:
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//url = webView.getUrl(); --remove this. You already have the URL in String url parameter
if (url.endsWith(".m3u8") || url.endsWith(".ts") || url.endsWith(".amv") || url.endsWith(".mp4"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse(url);
intent.setDataAndType( videoUri, "application/x-mpegURL" );
intent.setPackage( "com.mxtech.videoplayer.ad" );
startActivity( intent );
return true; // to tell the WebView that it should not load the URL because you handled it by yourself
}
//webView.loadUrl(url); remove this. You need to return true or false to tell the WebView if it should load the url, or not
return false; //to tell WebView that you didn't handle the URL, and it should load it.
}

Related

I want to disable a Image in webview. I think we can block the URL of image from loading, but How to achieve it

Link which I want to block https://learningwithsajal.xyz/wp-content/uploads/2021/06/PicsArt_06-24-07.49.43-removebg-preview.png
I am also adding code of my MainActivity.java
package com.learning.withsajal;
import ...
public class MainActivity extends AppCompatActivity {
String websiteURL = "https://learningwithsajal.xyz/"; // sets web url
private WebView webview;
SwipeRefreshLayout mySwipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if( ! CheckNetwork.isInternetAvailable(this)) //returns true if internet available
{
//if there is no internet do this
setContentView(R.layout.activity_main);
//Toast.makeText(this,"No Internet Connection, Chris",Toast.LENGTH_LONG).show();
new AlertDialog.Builder(this) //alert the person knowing they are about to close
.setTitle("No internet connection available")
.setMessage("Please Check you're Mobile data or Wifi network.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
//.setNegativeButton("No", null)
.show();
}
else
{
//Webview stuff
webview = findViewById(R.id.webView);
webview.setWebChromeClient(new ChromeClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl(websiteURL);
webview.setWebViewClient(new WebViewClientDemo());
}
}
What is solution of this problem ?
Hi to disable the images in webView you can use this code:
webView.getSettings().setLoadsImagesAutomatically(false);

How can I force a url parameter to all urls loaded in webview?

How would I go about applying a url parameter, ?theme=androidphone, to all urls loaded by this webview. I have included a snippet of the webview code in my android application to consider
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new Callback());
webView.loadUrl("https://www.welcometomywebsite.com/?theme=androidphone");
//webView.loadUrl("https://filebin.net/");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
if (Build.VERSION.SDK_INT >= 21) {
webSettings.setMixedContentMode(0);
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
To add parameter to all urls
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
//appending parameter to whatever url loading in web view
String newUrl = url + "?theme=androidphone";
//passing modified url to web view
webView.loadUrl(newUrl);
return false;
}
Note:
When the shouldOverrideUrlLoading() method returns false, the URLs passed as parameter to the method is loaded inside the WebView instead of the Android standard browser.
UPDATE:
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url){
//appending parameter to whatever url loading in web view
String newUrl = url + "?theme=androidphone";
//passing modified url to web view
webView.loadUrl(newUrl);
return false;
}
});
/*webView settings can be defined here*/
webView.loadUrl("https://www.welcometomywebsite.com/");

Get link of the clicked image in a webview

I want to get the link of an image clicked inside a Webview to display it in an new intent. The site that i want the image from is not opening the images in fullscreen when clicked.
I want something like the code down bellow but the website doesn't open images in the same tab.
Reference Code:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url == null){
return false;
}else if (url.trim().toLowerCase().endsWith(".img")) {//use whatever image formats you are looking for here.
String imageUrl = url;//here is your image url, do what you want with it
}else{
view.loadUrl(url);
}
}
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url == null){
return false;
}else if (url.trim().toLowerCase().endsWith(".img")) {//use whatever
image formats you are looking for here.
String imageUrl = url;//here is your image url, do what you want with
it
Intent intent = new Intent(this, NewActivity.class);
//pass from here the data to next activity and load there
intent.putExtra("yourURL", imageUrl);
startActivity(intent)
}else{
view.loadUrl(url);
}
}
}

Open files with other apps in webview

I'm building a simple app, in which I use a webview in Android Studio, in the app, i search a movie, and then I download the torrent.
When I download in the web, it automatically send me to the utorrent app, but when I download it from my app, I can't open it.
Sorry for my English and for my poor knowledge.
My java code:
public class MainActivity extends AppCompatActivity {
WebView web;
EditText et_peli;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web=(WebView)findViewById(R.id.web);
et_peli=(EditText)findViewById(R.id.et_peli);
}
public void buscar0nclick(View v){
WebSettings conf = web.getSettings();
conf.setJavaScriptEnabled(true);
web.loadUrl("http://www.miltorrents.com/?pTit=" + et_peli.getText().toString());
web.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".torrent")) {
Uri source = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(source);
request.setDescription("Description for the DownloadManager Bar");
request.setTitle("YourApp");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
else view.loadUrl(url);
return true;
}
});
}
}
Just to make sure I have this right, you want to send your user, once they download the torrent to the torrent's app to view the movie, correct?
I would check to see if that torrent app has a broadcast receiver that you can search for. If so, you could just pass along the information. If not, you have to look for a way to have that media file play within your app, or use one of the default movie players.

Nothing happens when I click a link inside my webview

This is a part of my android app, i have created a webview...but when i click on any link inside the webview...nothing happens....here is my code...i want to launch the link either in any browser or any installed download manager...i m a newbie..please help me out with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
setupActionBar();
// no need to use title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set webview as main content only
mWeb = new WebView(this);
setContentView(mWeb);
// set Javascript
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading data from web
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// set url for webview to load
mWeb.loadUrl("http://vesit-d7b.host22.com/Assignments/ECCF.html");
}
Please use this code and check that your url is being caught by the webview or not.
myweb.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
Toast.makeText(this,url,Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
see what url is coming when you click the link.....
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.v("here","the link is ::" + url);
view.loadUrl(url);
return true;
}
and then copy it on browser to see whether it is a valid link or not..
#Ritesh remove the shouldOverrideUrlLoading method it will work

Categories