I have an android app, there is an activity with a webView, it acts like a browser (it open some links I provided). Whenever I browse through my webView if there is a link that leads to play store to download an app my webView can't respond, it says 'webpage not available'. It can't send an intent to open play store or something like google chrome. How can I do it?
This works for me and opens play store app if it is installed and else shows playstore
link within the same webView.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
}
});
Related
before my app was working well in webview using a website link but now when i changed domain and url it doesn't working it opens in external browser all internal links too
please someone help me ,i need to upload it into playstore fast..`
private class WebViewClientDemo 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);
return true;
}else if (Uri.parse(url).getHost().equals("https://imovers.in") ) {
//open url contents in webView
return false;
} else{
//here open external links in external browser,phone or app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}`
FB share button could successfully oepn the FB messenger app in Chrome browser.
However, the same source code, it fail to open the FB messenger app if display in Android APP application, and this application display web page using Chrome browser inside.
How to fix it?
You could provide a custom WebViewClient implementation to your WebView that checks for facebook.com links (or whatever links you like) and then explicitly fire an Intent so others can pickup the Action, instead of allowing the WebView to handle it as it sees fit.
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
Uri uri = Uri.parse(url);
if (uri.getHost().contains("facebook.com")) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return false;
}
} catch(Exception e){
e.printStackTrace();
}
return super.shouldOverrideUrlLoading(view, url);
}
});
I have made a simple app from website with webView. It's a torrent site. I post magnet link in the site. What I want is when I click on only magnet link all torrent app like bit-torrent will catch the address automatically. Plus all other external site link will open in external browser like Chrome.
I have followed some online tutorial even from here (stackoverflow) but they are old and used shouldOverrideUrlLoading, but Google says that this method was deprecated in API level 24.
Here https://developer.android.com/guide/webapps/webview.html
I have followed google to use this code.(modified to match with my site) but its not working. please someone help me with this.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
This my java main activity code now.now every link in the site open in webview but I don't want that and for magnet link it shows like this snapshot.
public class MainActivity extends Activity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
// Configure related browser settings
myWebView.getSettings().setLoadsImagesAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
// Configure the client to use when opening URLs
myWebView.setWebViewClient(new MyBrowser());
// Load the initial URL
myWebView.loadUrl("https://example.com");
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
private class MyBrowser extends WebViewClient {
}
}
public boolean isAppInstalled(String packageName) {
try {
activity.getPackageManager().getApplicationInfo(
packageName, 0);
return true;
} catch (ActivityNotFoundException ex1) {
return false;
} catch (PackageManager.NameNotFoundException ex2) {
return false;
} catch (NullPointerException ex3) {
return false;
} catch (Exception ex4) {
return false;
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith.("magnet")) {
if(isAppInstalled('com.bittorrent.client')){
PackageManager pm = getApplicationContext().getPackageManager();
Intent appStartIntent =
pm.getLaunchIntentForPackage('com.bittorrent.client');
getApplicationContext().startActivity(appStartIntent);
}else{
// No matching app found, toast it.
}
}
return true;
}
}
use this: updated with Nougut support. Remember to upgrade your sdk api level to 25 in Android studio. otherwise you could get error.
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startsWith("magnet")) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
Toast.makeText(getBaseContext(), "Team localpeers: No bittorent client installed in system.", Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url=request.getUrl().toString();
if (url.startsWith("magnet")) { Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
Toast.makeText(getBaseContext(), "Team localpeers: No bittorent client installed in Android.", Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
I had the same problem with the magnet links, I remembered a while ago I manage to link the magnet link simply by loading the url from the webview, unfortunately this solution didn't worked on my phone (One Plus 5 running on Oreo).
My solution to this issue:
You need to scrape the url or urls you want to show to the user. In my case I have a recycle view where the user can chose one item (movie) which he can download. In order to scrape the data I used those libraries:
implementation "pl.droidsonroids:jspoon:1.3.0"
implementation "pl.droidsonroids.retrofit2:converter-jspoon:1.3.0"
You can check here on how actually you need to use those libraries from here link.
After you scrape your data you need to show to the user a list (in my case) of the items that the user can download, when the user click on one of the items you basically use the
magnet url that you scraped before and apply simply this code:
String url = "your magent url";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
No need to use any webviews or custom WebViewClients.
A small note (I checked if the user have a torrent application before he could manage to download any torrents you simply use the packagemanager and check if any of his applications contains torrent).
I only checked this solution on my phone
I have a Web App that loads locally html.
In one html, I have a link to a Google Play app, but the link opens inside the app, inside the webview.
On iOS devices any link https://itunes.apple.com/app/id000 will automatically open iTunes/App Store on the devices.
Is there a way to open the Google Play app directly from an html?
I'm trying _blank or the market:// as suggested on other questions, but nothing works.
App Link
App Link 2
Thanks!
you can use this it will work
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
// Google Play app is not installed, you may want to open the app store link
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
}});
maybe this will help any one
In my WebView I'm loading YouTube official website:
http://m.youtube.com/index?desktop_uri=%2F&gl=US#/
I want the user to be able to choose a video and simply watch it. The WebView doesn't do it so I thought that maybe I should run an intent to youtube app.
First, I had to export the videoID from the link.
I tried this code, but it doesn't match the link..:
public class BrowserClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//Check if YouTube video link
Log.d("WBC", "loading link: " + url);
String youTubeLink = "http://m.youtube.com/index?desktop_uri=%2F&gl=US#/watch?xl=xl_blazer&v=";
if (url.startsWith(youTubeLink)){
Log.d("WBC", "This is a YouTube link: " + url);
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("http://www.youtube.com/v/%s", url.substring(youTubeLink.length(),url.length())))));
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
}
Any help?
Please try this code, it works in mine apps perfectly.
String sPath = "urlGoesHere";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(sPath));
startActivity(i);