Ich versuche, eine App für die Webanwendung meines Unternehmens zu machen, und es funktioniert gut. Das einzige Problem ist, dass ich einige Links habe, die aus verschiedenen Domains stammen, wie Youtube, das einige Videos öffnet. Alle anderen Links, die sich auf die Domain beziehen, die als meine Domain angegeben ist, werden im Webview geöffnet und die Website in meiner App angezeigt. Der Youtube-Link und andere externe Website-Links funktionieren jedoch nicht.Webview öffnet keine Links von verschiedenen Domains in Chrome Android
Wenn ich mich erinnere, sollte es den Benutzer fragen, ob er es mit Chrome oder Youtube oder der Facebook-App basierend auf dem Link öffnen möchte, aber es funktioniert nicht.
Hier ist meine WebView Code:
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);
}
}
ich alles versucht, aber es lässt sich nicht öffnen Links aus verschiedenen Bereichen im Web-Browser. Was ist falsch?
es sagt nicht Symbol-Aktivität lösen. Ich habe keine Ahnung, welche Aktivität ich hier nennen soll. –
mach es einfach this.startActivity (intent) – user5434084
Das hat bei mir funktioniert, danke! – JoeYo