mWebView.setWebViewClient(new CommentWebViewClient());
public class CommentWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return openApp(url);
}
}
//判断app是否安装
private boolean isInstall(Intent intent) {
return MLApplication.getInstance().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
}
//打开app
private boolean openApp(String url) {
if (TextUtils.isEmpty(url)) return false;
try {
if (!url.startsWith("http") && !url.startsWith("https") && !url.startsWith("ftp")) {
Uri uri = Uri.parse(url);
String host = uri.getHost();
String scheme = uri.getScheme();
//host 和 scheme 都不能为null
if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(scheme)) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (isInstall(intent)) {
startActivity(intent);
return true;
}
}
}
} catch (Exception e) {
return false;
}
return false;
}