【问题标题】:Check if app on the device for intent is installed or not检查设备上是否安装了意图应用程序
【发布时间】:2017-04-25 08:06:53
【问题描述】:

我正在开发加载我的网站的 WebView。 我的问题是“分享”按钮,其网址不合法。 我试图通过应用程序来解决这个问题,但它不起作用。

我不知道如何检查设备上是否安装了应用程序。 我的问题是,当设备上未安装单击共享的应用时,我的应用已停止。

这是我的代码:

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);*/

        }

【问题讨论】:

    标签: android android-intent webview


    【解决方案1】:
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
             try{
                // 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);
                }
    
             }
            catch(Exception ignored){
        Toast.makeText(getParent(), "no app found to open this link!", 
       Toast.LENGTH_LONG).show();
    
              }
           return super.shouldOverrideUrlLoading(view, url);
            }
    

    可以这样做

    【讨论】:

      【解决方案2】:

      添加一个工具来判断这个应用程序是否存在。 (AndroidManifest.xml 中需要一个条件)

      Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
      mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
      appInfos = packageManager.queryIntentActivities(mainIntent, 1);
      

      【讨论】:

        【解决方案3】:

        在开始意图之前添加以下检查:

         // 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();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-25
          • 2014-09-21
          • 2021-04-03
          • 1970-01-01
          • 1970-01-01
          • 2013-09-16
          • 2011-10-14
          相关资源
          最近更新 更多