Open Facebook page from Android app?

前端 未结 26 3041
被撕碎了的回忆
被撕碎了的回忆 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:25
    try {
           String[] parts = url.split("//www.facebook.com/profile.php?id=");
           getPackageManager().getPackageInfo("com.facebook.katana", 0);
           startActivity(new Intent (Intent.ACTION_VIEW, Uri.parse(String.format("fb://page/%s", parts[1].trim()))));
        } catch (Exception e) {
           startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    
    0 讨论(0)
  • 2020-11-22 07:26

    You can open the facebook app on button click as follows:-

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        this.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                startNewActivity("com.facebook.katana");
            }
        });
    
    }
    
    public void startNewActivity( String packageName)
    {
        Intent intent = MainActivity.this.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent != null)
        {
            // we found the activity
            // now start the activity
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
        else
        {
            // bring user to the market
            // or let them choose an app?
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse("market://details?id="+packageName));
            startActivity(intent);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:27

    To do this we need the "Facebook page id", you can get it :

    • from the page go to "About".
    • go to "More Info" section.

    To opening facebook app on specified profile page,

    you can do this:

     String facebookId = "fb://page/<Facebook Page ID>";
      startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));
    

    Or you can validate when the facebook app is not installed, then open the facebook web page.

    String facebookId = "fb://page/<Facebook Page ID>";
    String urlPage = "http://www.facebook.com/mypage";
    
         try {
              startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));
            } catch (Exception e) {
             Log.e(TAG, "Application not intalled.");
             //Open url web page.
             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
            }
    
    0 讨论(0)
  • 2020-11-22 07:27

    Declare constants

      private String FACEBOOK_URL="https://www.facebook.com/approids";
        private String FACEBOOK_PAGE_ID="approids";
    

    Declare Method

    public String getFacebookPageURL(Context context) {
            PackageManager packageManager = context.getPackageManager();
            try {
                int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
    
                boolean activated =  packageManager.getApplicationInfo("com.facebook.katana", 0).enabled;
                if(activated){
                    if ((versionCode >= 3002850)) {
                        Log.d("main","fb first url");
                        return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
                    } else {
                        return "fb://page/" + FACEBOOK_PAGE_ID;
                    }
                }else{
                    return FACEBOOK_URL;
                }
            } catch (PackageManager.NameNotFoundException e) {
                return FACEBOOK_URL;
            }
        }
    

    Call Function

    Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
                    String facebookUrl = getFacebookPageURL(MainActivity.this);
                    facebookIntent.setData(Uri.parse(facebookUrl));
                    startActivity(facebookIntent);
    
    0 讨论(0)
  • 2020-11-22 07:29

    After much testing I have found one of the most effective solutions:

    private void openFacebookApp() {
        String facebookUrl = "www.facebook.com/XXXXXXXXXX";
        String facebookID = "XXXXXXXXX";
    
        try {
            int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
    
            if(!facebookID.isEmpty()) {
                // open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
                Uri uri = Uri.parse("fb://page/" + facebookID);
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            } else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
                // open Facebook app using facebook url
                Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            } else {
                // Facebook is not installed. Open the browser
                Uri uri = Uri.parse(facebookUrl);
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            }
        } catch (PackageManager.NameNotFoundException e) {
            // Facebook is not installed. Open the browser
            Uri uri = Uri.parse(facebookUrl);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:34

    Here's the way to do it in 2016, works great, and is very easy.

    I discovered this after looking into how emails send by facebook opened the app.

    // e.g. if your URL is https://www.facebook.com/EXAMPLE_PAGE, you should put EXAMPLE_PAGE at the end of this URL, after the ?
    String YourPageURL = "https://www.facebook.com/n/?YOUR_PAGE_NAME";
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(YourPageURL));
    
    startActivity(browserIntent);
    
    0 讨论(0)
提交回复
热议问题