简体   繁体   中英

How to open the Facebook App with an Android app?

I am trying to open the Facebook App or Facebook's page with an android app but I can't do it. It doesn't do anything.

Here is the code:

public Intent getOpenFacebookIntent(Context context){

    try{
        context.getPackageManager().getPackageInfo("com.facebook.FacebookSdk", 0);
        return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/ID"));
    } catch (Exception e) {
        return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/my_page"));
    }
}

@Override
public void onClick (View v){
    switch (v.getId()) {
        case R.id.guardar:
          startActivity(new Intent(this, getOpenFacebookIntent(this)));
        break;

Can someone help me please?

Package name should be this.

details?id=com.facebook.katana

Chage this.

context.getPackageManager().getPackageInfo("com.facebook.FacebookSdk", 0);

To this.

context.getPackageManager().getPackageInfo("com.facebook.katana", 0);

EDIT1:

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

Call: launchFacebook(); in your onClick() method

public void launchFacebook() {
    String urlFb = "fb://page/"+ID;
    // replace ID with your id 
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(urlFb));

    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
    if (list.size() == 0) {
        final String urlBrowser = "https://www.facebook.com/"+my_page; 
        // relpace my_page with facebook profile page ending
        intent.setData(Uri.parse(urlBrowser));
    }
    startActivity(intent);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM