Open Facebook page from Android app?

前端 未结 26 3126
被撕碎了的回忆
被撕碎了的回忆 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:43

    1-to get your id go to your image profile and click right click and take copy link adress.

            try {
                    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("fb://profile/id"));
                    startActivity(intent);
                } catch(Exception e) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("facebook url")));
                }
            }
        });
    
    0 讨论(0)
  • 2020-11-22 07:44

    Is this not easier? For example within an onClickListener?

    try {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/426253597411506"));
        startActivity(intent);
    } catch(Exception e) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/appetizerandroid")));
    }
    

    PS. Get your id (the large number) from http://graph.facebook.com/[userName]

    0 讨论(0)
  • 2020-11-22 07:44

    My answer builds on top of the widely-accepted answer from joaomgcd. If the user has Facebook installed but disabled (for example by using App Quarantine), this method will not work. The intent for the Twitter app will be selected but it will not be able to process it as it is disabled.

    Instead of:

    context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
    return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
    

    You can use the following to decide what to do:

    PackageInfo info = context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
    if(info.applicationInfo.enabled)
        return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
    else
        return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/620681997952698"));
    
    0 讨论(0)
  • 2020-11-22 07:44

    Best answer I have found, it's working great.

    Just go to your page on Facebook in the browser, right click, and click on "View source code", then find the page_id attribute: you have to use page_id here in this line after the last back-slash:

    fb://page/pageID
    

    For example:

    Intent facebookAppIntent;
    try {
        facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/1883727135173361"));
        startActivity(facebookAppIntent);
    } catch (ActivityNotFoundException e) {
        facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/CryOut-RadioTv-1883727135173361"));
        startActivity(facebookAppIntent);
    }
    
    0 讨论(0)
  • 2020-11-22 07:45

    A more reusable approach.

    This is a functionality we generally use in most of our apps. Hence here is a reusable piece of code to achieve this.

    (Similar to other answers in terms for facts. Posting it here just to simplify and make the implementation reusable)

    "fb://page/ does not work with newer versions of the FB app. You should use fb://facewebmodal/f?href= for newer versions. (Like mentioned in another answer here)

    This is a full fledged working code currently live in one of my apps:

    public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
    public static String FACEBOOK_PAGE_ID = "YourPageName";
    
    //method to get the right URL to use in the intent
    public String getFacebookPageURL(Context context) {
            PackageManager packageManager = context.getPackageManager();
            try {
                int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
                if (versionCode >= 3002850) { //newer versions of fb app
                    return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
                } else { //older versions of fb app
                    return "fb://page/" + FACEBOOK_PAGE_ID;
                }
            } catch (PackageManager.NameNotFoundException e) {
                return FACEBOOK_URL; //normal web url
            }
        }
    

    This method will return the correct url for app if installed or web url if app is not installed.

    Then start an intent as follows:

    Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
    String facebookUrl = getFacebookPageURL(this);
    facebookIntent.setData(Uri.parse(facebookUrl));
    startActivity(facebookIntent);
    

    That's all you need.

    0 讨论(0)
  • 2020-11-22 07:45

    As of March 2020 this works perfectly.

    private void openFacebookPage(String pageId) {
        String pageUrl = "https://www.facebook.com/" + pageId;
    
        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.katana", 0);
    
            if (applicationInfo.enabled) {
                int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
                String url;
    
                if (versionCode >= 3002850) {
                    url = "fb://facewebmodal/f?href=" + pageUrl;
                } else {
                    url = "fb://page/" + pageId;
                }
    
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            } else {
                throw new Exception("Facebook is disabled");
            }
        } catch (Exception e) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(pageUrl)));
        }
    }
    
    0 讨论(0)
提交回复
热议问题