@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Intent intent; // Perform generic parsing of the URI to turn it into an Intent. try { intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); } catch (URISyntaxException ex) { Log.w(TAG, "Bad URI " + url + ": " + ex.getMessage()); return false; } // Sanitize the Intent, ensuring web pages can not bypass browser // security (only access to BROWSABLE activities). intent.addCategory(Intent.CATEGORY_BROWSABLE); intent.setComponent(null); Intent selector = intent.getSelector(); if (selector != null) { selector.addCategory(Intent.CATEGORY_BROWSABLE); selector.setComponent(null); } // Pass the package name as application ID so that the intent from the // same application can be opened in the same tab. intent.putExtra(Browser.EXTRA_APPLICATION_ID, view.getContext().getPackageName()); try { view.getContext().startActivity(intent); } catch (ActivityNotFoundException ex) { Log.w(TAG, "No application can handle " + url); return false; } return true; }
private static boolean startBrowsingIntent(Context context, String url) { Intent intent; // Perform generic parsing of the URI to turn it into an Intent. try { intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); } catch (Exception ex) { Log.w(TAG, "Bad URI %s", url, ex); return false; } // Check for regular URIs that WebView supports by itself, but also // check if there is a specialized app that had registered itself // for this kind of an intent. Matcher m = BROWSER_URI_SCHEMA.matcher(url); if (m.matches() && !isSpecializedHandlerAvailable(context, intent)) { return false; } // Sanitize the Intent, ensuring web pages can not bypass browser // security (only access to BROWSABLE activities). intent.addCategory(Intent.CATEGORY_BROWSABLE); intent.setComponent(null); Intent selector = intent.getSelector(); if (selector != null) { selector.addCategory(Intent.CATEGORY_BROWSABLE); selector.setComponent(null); } // Pass the package name as application ID so that the intent from the // same application can be opened in the same tab. intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); try { context.startActivity(intent); return true; } catch (ActivityNotFoundException ex) { Log.w(TAG, "No application can handle %s", url); } return false; }