【发布时间】:2017-12-31 08:12:28
【问题描述】:
我用 webView 从网站制作了一个简单的应用程序。这是一个洪流网站。我在网站上发布了磁铁链接。我想要的是,当我只点击磁力链接时,所有像 bit-torrent 这样的 torrent 应用程序都会自动捕获地址。此外,所有其他外部网站链接都将在 Chrome 等外部浏览器中打开。
我什至从这里 (stackoverflow) 开始学习一些在线教程,但它们很旧并且使用了 shouldOverrideUrlLoading,但 Google 表示此方法在 API 级别 24 中已弃用。
这里https://developer.android.com/guide/webapps/webview.html 我已跟随谷歌使用此代码。(修改为与我的网站匹配)但它不起作用。请有人帮我解决这个问题。
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;
}
}
现在这是我的 java 主要活动代码。现在网站中的每个链接都在 webview 中打开,但我不希望这样,对于磁力链接,它显示如下快照。
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 {
}
}
【问题讨论】:
标签: android url android-intent android-webview magnet-uri