创建IF ELSE语句以在应用存在时执行操作-更新

时间:2019-06-29 13:06:22

标签: java android facebook android-intent webview

几周前我有一个问题( my previous question),现在我再次需要该Web应用程序的帮助。

从那时起,我添加了很多意图和一些通知功能,但是我遇到了一个很大的问题。

如果该意图的应用程序不存在,则我的应用程序崩溃。我想发表一个声明,如果该应用程序不存在,则可以在同一Web视图中打开该网站。我想用youtube,facebook,instagram,twitter和google地图来做到这一点。 我也想为GO应用程序和lite应用程序添加相同的功能。

例如,如果存在facebook,请在facebook中打开该意图。如果facebook不存在,但是facebook lite存在,请在facebook lite中打开它。如果不存在facebook和facebook lite,请在webview中打开facebook


更新:

public class MainActivity extends AppCompatActivity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myWebView = findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("https://www.maximus.com.ar");
    myWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView wView, String url)
        {
            if (url.startsWith("mailto:") || url.startsWith("tel:") || url.startsWith("geo:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(url));
                startActivity(intent);
                return true;
            } else if (url.startsWith("whatsapp:")) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_VIEW);
                sendIntent.setPackage("com.whatsapp");
                sendIntent.setData(Uri.parse(
                        url));
                startActivity(sendIntent);
                return true;
            } else if (url.contains("facebook")) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_VIEW);
                sendIntent.setPackage("com.facebook.katana");
                sendIntent.setData(Uri.parse("fb://facewebmodal/f?href=" + url));
                startActivity(sendIntent);
                return true;
            } else if (url.startsWith("fb:")) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_VIEW);
                sendIntent.setPackage("com.facebook.katana");
                sendIntent.setData(Uri.parse(url));
                startActivity(sendIntent);
                return true;
            } else if (url.contains("twitter")) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_VIEW);
                sendIntent.setPackage("com.twitter.android");
                sendIntent.setData(Uri.parse(url));
                startActivity(sendIntent);
                return true;
            } else if (url.contains("netone")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(
                        url));
                startActivity(intent);
                return true;
            } else if (url.contains("afip")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(
                        url));
                startActivity(intent);
                return true;
            } else if (url.contains("youtube")) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_VIEW);
                sendIntent.setPackage("com.google.android.youtube");
                sendIntent.setData(Uri.parse(
                        url));
                startActivity(sendIntent);
                return true;
            } else if (url.contains("maps")) {
                Intent sendIntent = new Intent();
                if (sendIntent.resolveActivity(getPackageManager()) == null) {
                    // No Activity found that can handle this intent.
                }
                else{
                    // There is an activity which can handle this intent.
                    sendIntent.setAction(Intent.ACTION_VIEW);
                    sendIntent.setPackage("com.google.android.apps.maps");
                    sendIntent.setData(Uri.parse(
                            url));
                    startActivity(sendIntent);
                    return true;
                }
            } else if (url.contains("instagram")) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_VIEW);
                sendIntent.setPackage("com.instagram.android");
                sendIntent.setData(Uri.parse(
                        url));
                startActivity(sendIntent);
                return true;
            } else if (url.contains("baires")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(
                        url));
                startActivity(intent);
                return true;
            } else if (url.startsWith("spotify:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(
                        url));
                intent.putExtra(Intent.EXTRA_REFERRER,
                        Uri.parse("android-app://" + getPackageName()));
                startActivity(intent);
                return true;
            }
            return false;
        }
    });
}
@Override
public void onBackPressed() {
    if(myWebView.canGoBack()) {
        myWebView.goBack();
    } else {
        super.onBackPressed();}
    }
}

就像我之前说的那样,如果该应用程序不存在,我将无法实现打开该意图的webview。不断崩溃。

尝试以下操作,但即使该应用存在,也只能在Web视图中打开该意图:

else if (url.contains("facebook")) {
                Intent sendIntent = new Intent();
                if (sendIntent.resolveActivity(getPackageManager()) == null) {
                    // No Activity found that can handle this intent.
                }
                else{
                    sendIntent.setAction(Intent.ACTION_VIEW);
                    sendIntent.setPackage("com.facebook.katana");
                    sendIntent.setData(Uri.parse("fb://facewebmodal/f?href=" + url));
                    startActivity(sendIntent);
                    return true;
                }
            }

0 个答案:

没有答案