Android 检查设备上的应用程序是否有意图

Android 检查设备上的应用程序是否有意图,android,android-intent,webview,Android,Android Intent,Webview,我在加载网站的webview上工作。 但我的问题在于“共享”按钮——它们的url不合法。 我通过尝试应用程序解决了这个问题,但我的代码有问题: 我不知道要检查设备上是否安装了应用程序。 我的问题是,当点击共享的应用程序没有启动时,应用程序就会停止 这是我的代码: public boolean shouldOverrideUrlLoading(WebView view, String url) { // HERE YOU GET url Progre

我在加载网站的webview上工作。 但我的问题在于“共享”按钮——它们的url不合法。 我通过尝试应用程序解决了这个问题,但我的代码有问题: 我不知道要检查设备上是否安装了应用程序。 我的问题是,当点击共享的应用程序没有启动时,应用程序就会停止

这是我的代码:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // HERE YOU GET url
            ProgressBar PB1 = (ProgressBar) findViewById(R.id.progressBar2);
            PB1.setVisibility(View.VISIBLE);
            if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
                return super.shouldOverrideUrlLoading(view, url);
            } else {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

                view.getContext().startActivity(intent);
                browser("javascript:window.location.reload(true)");
                return true;/// super.shouldOverrideUrlLoading(view, url);
            }
                    /*Toast.makeText(getParent(), "no app found to open this link!", Toast.LENGTH_LONG).show();
                    return super.shouldOverrideUrlLoading(view, url);*/

        }

可以这样做

添加一个utils以判断此应用程序是否存在。(您需要AndroidManifest.xml中的条件)


在启动前添加以下检查:

 // Verify that the intent will resolve to an activity
        if (sendIntent.resolveActivity(getPackageManager()) != null) {
            view.getContext().startActivity(intent);
        } else{
            Toast.makeText(getParent(), "no app found to open this link!", Toast.LENGTH_LONG).show();
}
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
appInfos = packageManager.queryIntentActivities(mainIntent, 1);
 // Verify that the intent will resolve to an activity
        if (sendIntent.resolveActivity(getPackageManager()) != null) {
            view.getContext().startActivity(intent);
        } else{
            Toast.makeText(getParent(), "no app found to open this link!", Toast.LENGTH_LONG).show();
}