Webview 未打开 chrome Android 中不同域的链接
Webview is not opening links of different domains in chrome Android
我正在尝试为我公司的 Web 应用程序制作一个应用程序,并且运行良好。唯一的问题是我有一些来自不同域的 links,比如 Youtube,它打开了一些视频。与指定为我的域的域相关的所有其他 link 将在 Web 视图中打开并在我的应用程序中显示该网站。但是 Youtube link 和其他外部网站 link 不起作用。
如果我记得,它应该询问用户是否想使用 Chrome 或 Youtube 或基于 link 的 Facebook 应用程序打开它,但它不起作用。
这是我的 WebView 代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com/");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setSupportMultipleWindows(true);
myWebView.setHorizontalScrollBarEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true); // This will allow access
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().startsWith("google.com")) {
view.loadUrl(url);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
return true;
}
});
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
WebView myWebView = (WebView) findViewById(R.id.webview);
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
我已经尝试了所有方法,但无法在 Web 浏览器中打开来自不同域的 links。怎么了?
也许你可以在 shouldOverrideUrlLoading 方法中尝试这个来检查它的公司域,它应该通过这种方式在 webView 中打开。它对我有用。
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.indexOf("yourcompanydomain.com") > -1 ) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
或尝试
if(myWebView.getUrl().startsWith("yourcompanydomain.com")){
return false ;
}
else
{
Intent....
}
我正在尝试为我公司的 Web 应用程序制作一个应用程序,并且运行良好。唯一的问题是我有一些来自不同域的 links,比如 Youtube,它打开了一些视频。与指定为我的域的域相关的所有其他 link 将在 Web 视图中打开并在我的应用程序中显示该网站。但是 Youtube link 和其他外部网站 link 不起作用。
如果我记得,它应该询问用户是否想使用 Chrome 或 Youtube 或基于 link 的 Facebook 应用程序打开它,但它不起作用。
这是我的 WebView 代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com/");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setSupportMultipleWindows(true);
myWebView.setHorizontalScrollBarEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true); // This will allow access
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().startsWith("google.com")) {
view.loadUrl(url);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
return true;
}
});
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
WebView myWebView = (WebView) findViewById(R.id.webview);
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
我已经尝试了所有方法,但无法在 Web 浏览器中打开来自不同域的 links。怎么了?
也许你可以在 shouldOverrideUrlLoading 方法中尝试这个来检查它的公司域,它应该通过这种方式在 webView 中打开。它对我有用。
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.indexOf("yourcompanydomain.com") > -1 ) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
或尝试
if(myWebView.getUrl().startsWith("yourcompanydomain.com")){
return false ;
}
else
{
Intent....
}