Open Facebook page from Android app?

前端 未结 26 3135
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 07:03

from my Android app, I would like to open a link to a Facebook profile in the official Facebook app (if the app is installed, of course). For iPhone, there exists the

26条回答
  •  鱼传尺愫
    2020-11-22 07:46

    I implemented in this form in webview using fragment- inside oncreate:

     webView.setWebViewClient(new WebViewClient()
    {
        public boolean shouldOverrideUrlLoading(WebView viewx, String urlx)
                    {
         if(Uri.parse(urlx).getHost().endsWith("facebook.com")) {
                            {
                                goToFacebook();
                            }
                            return false;
                        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
                        viewx.getContext().startActivity(intent);
                        return true;
                    }
    
    });
    

    and outside onCreateView :

     private void goToFacebook() {
            try {
                String facebookUrl = getFacebookPageURL();
                Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
                facebookIntent.setData(Uri.parse(facebookUrl));
                startActivity(facebookIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //facebook url load
        private String getFacebookPageURL() {
            String FACEBOOK_URL = "https://www.facebook.com/pg/XXpagenameXX/";
            String facebookurl = null;
    
            try {
                PackageManager packageManager = getActivity().getPackageManager();
    
                if (packageManager != null) {
                    Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");
    
                    if (activated != null) {
                        int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
    
                        if (versionCode >= 3002850) {
                            facebookurl = "fb://page/XXXXXXpage_id";
                        }
                    } else {
                        facebookurl = FACEBOOK_URL;
                    }
                } else {
                    facebookurl = FACEBOOK_URL;
                }
            } catch (Exception e) {
                facebookurl = FACEBOOK_URL;
            }
            return facebookurl;
        }
    

提交回复
热议问题