android - Need help to make site with webView with external link handling like "magnet" -
i have made simple app website webview. it's torrent site. post magnet link in site. want when click on magnet link torrent app bit-torrent catch address automatically. plus other external site link open in external browser chrome.
i have followed online tutorial here (stackoverflow) old , used shouldoverrideurlloading
, google says method deprecated in api level 24.
here https://developer.android.com/guide/webapps/webview.html have followed google use code.(modified match site) not working. please me this.
private class mywebviewclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview view, string url) { if (uri.parse(url).gethost().equals("www.example.com")) { // web site, not override; let webview load page return false; } // otherwise, link not page on site, launch activity handles urls intent intent = new intent(intent.action_view, uri.parse(url)); startactivity(intent); return true; } }
this java main activity code now.now every link in site open in webview don't want , magnet link shows 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 client use when opening urls mywebview.setwebviewclient(new mybrowser()); // load 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; } }
Comments
Post a Comment