我使用webView的網站做了一個簡單的應用程序。這是一個洪流網站。我在該網站發佈磁鏈接。我想要的是當我只點擊磁鐵鏈接時,所有洪流應用程序就像bit-torrent會自動捕捉地址。此外,所有其他外部網站鏈接都將在Chrome等外部瀏覽器中打開。需要幫助,使webView與外部鏈接處理,如「磁鐵」
我按照一些在線教程,甚至從這裏(計算器),但他們都老了,而且shouldOverrideUrlLoading
,但谷歌表示,這種方法是在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的主要活動代碼now.now在該網站中的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 {
}
}
hlw謝謝你的代碼讚賞,但我有一些問題與code.sorry說,即時通訊非常新的。 '公佈爾isAppInstalled(字符串的packageName){ 嘗試{ activity.getPackageManager()getApplicationInfo( 的packageName,0);' 此活動顯示,如果以紅色 –
「不能解析符號活動」,在這裏「( url.startsWith。(「magnet」))' here startsWith顯示「無法解析符號startsWith」紅色。 –
活動是您當前活動的實例,您也可以直接在活動代碼中使用getPackageManager()。只需清理代碼,因爲第二個註釋的問題非常奇怪且意外。 –