从Android应用打开Facebook页面?

时间:2011-01-26 22:28:55

标签: android facebook url-scheme

从我的Android应用程序中,我想在官方Facebook应用程序中打开Facebook个人资料的链接(当然,如果安装了应用程序)。对于iPhone,存在fb://网址方案,但在我的Android设备上尝试相同的操作会引发ActivityNotFoundException

是否有机会通过代码在官方Facebook应用中打开Facebook个人资料?

29 个答案:

答案 0 :(得分:231)

这适用于最新版本:

  1. 转到https://graph.facebook.com/<user_name_here&gt; (例如https://graph.facebook.com/fsintents
  2. 复制您的身份
  3. 使用此方法:

    public static Intent getOpenFacebookIntent(Context context) {
    
       try {
        context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
        return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id_here>"));
       } catch (Exception e) {
        return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name_here>"));
       }
    }
    
  4. 如果用户已安装Facebook应用,则会打开该应用。否则,它将在浏览器中打开Facebook。

    编辑:自版本11.0.0.11.23(3002850)Facebook App不再支持这种方式了,还有另外一种方法,请查看以下来自Jared Rummler的回复。

答案 1 :(得分:136)

在Facebook版本11.0.0.11.23(3002850)fb://profile/fb://page/不再有效。我反编译Facebook应用程序,发现你可以使用fb://facewebmodal/f?href=[YOUR_FACEBOOK_PAGE]。这是我在制作中使用的方法:

/**
 * <p>Intent to open the official Facebook app. If the Facebook app is not installed then the
 * default web browser will be used.</p>
 *
 * <p>Example usage:</p>
 *
 * {@code newFacebookIntent(ctx.getPackageManager(), "https://www.facebook.com/JRummyApps");}
 *
 * @param pm
 *     The {@link PackageManager}. You can find this class through {@link
 *     Context#getPackageManager()}.
 * @param url
 *     The full URL to the Facebook page or profile.
 * @return An intent that will open the Facebook page/profile.
 */
public static Intent newFacebookIntent(PackageManager pm, String url) {
  Uri uri = Uri.parse(url);
  try {
    ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
    if (applicationInfo.enabled) {
      // http://stackoverflow.com/a/24547437/1048340
      uri = Uri.parse("fb://facewebmodal/f?href=" + url);
    }
  } catch (PackageManager.NameNotFoundException ignored) {
  }
  return new Intent(Intent.ACTION_VIEW, uri);
}

答案 2 :(得分:36)

这不容易吗? 例如在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。从http://graph.facebook.com/ [userName]

获取您的ID(大号)

答案 3 :(得分:30)

对于Facebook页面:

try {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/" + pageId));
} catch (Exception e) {
    intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + pageId));
}

对于Facebook个人资料:

try {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + profileId));
} catch (Exception e) {
    intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + profileId));
}

...因为没有一个答案指出差异

两者均在Facebook v.27.0.0.24.15和Nexus 4上的Android 5.0.1上进行了测试

答案 4 :(得分:27)

这是2016年的方法,效果很好,非常简单。

在查看facebook发送的电子邮件如何打开应用程序后,我发现了这一点。

// 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);

答案 5 :(得分:21)

这是执行此操作的最简单的代码

public final void launchFacebook() {
        final String urlFb = "fb://page/"+yourpageid;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(urlFb));

        // If a Facebook app is installed, use it. Otherwise, launch
        // a browser
        final PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
        if (list.size() == 0) {
            final String urlBrowser = "https://www.facebook.com/pages/"+pageid;
            intent.setData(Uri.parse(urlBrowser));
        }

        startActivity(intent);
    }

答案 6 :(得分:15)

更可重用的方法。

这是我们在大多数应用中通常使用的功能。因此,这是一个可重用的代码来实现这一目标。

(类似于事实方面的其他答案。在此发布只是为了简化并使实现可重复使用)

"fb://page/不适用于较新版本的FB应用。对于较新的版本,您应该使用fb://facewebmodal/f?href=。 (在此处的另一个答案中提及

这是一个完整的工作代码,目前存在于我的某个应用中:

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
        }
    }

如果安装了app,此方法将返回正确的app url;如果未安装app,则返回web url。

然后按如下方式启动意图:

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

这就是你所需要的一切。

答案 7 :(得分:9)

<Route exact={true} path="/" render={() => ( <div> <App /> <Route exact={true} path="/product/:id" component={Product}/> </div> )} /> 不适用于较新版本的FB应用。对于较新的版本,您应该使用"fb://page/

这是一个完整的工作代码:

fb://facewebmodal/f?href=

如果安装了app,此方法将返回正确的app url;如果未安装app,则返回web url。

然后按如下方式启动意图:

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
        }
    }

答案 8 :(得分:9)

这已经是reverse-engineered by Pierre87 on the FrAndroid forum,但我无法找到描述它的任何官方,因此必须将其视为无证件,并且可能随时停止工作:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana", "com.facebook.katana.ProfileTabHostActivity");
intent.putExtra("extra_user_id", "123456789l");
this.startActivity(intent);

答案 9 :(得分:7)

试试这段代码:

String facebookUrl = "https://www.facebook.com/<id_here>";
        try {
            int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
            if (versionCode >= 3002850) {
                Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
                   startActivity(new Intent(Intent.ACTION_VIEW, uri));
            } else {
                Uri uri = Uri.parse("fb://page/<id_here>");
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            }
        } catch (PackageManager.NameNotFoundException e) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
        }

答案 10 :(得分:6)

截至 2020年3月,此方法运行正常。

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)));
    }
}

答案 11 :(得分:6)

要做到这一点,我们需要“Facebook页面ID”,你可以得到它:

  • 从页面转到“关于”。
  • 转到“更多信息”部分。

introducir la descripción de la imagen aquí

要在指定的个人资料页面上打开Facebook应用

你可以这样做:

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

或者您可以验证未安装Facebook应用程序的时间,然后打开Facebook网页。

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(url)));
        }

答案 12 :(得分:5)

经过多次测试后,我找到了最有效的解决方案之一:

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));
    }
}

答案 13 :(得分:4)

我发现最佳答案,效果很好。

只需在浏览器中访问Facebook上的页面,右键单击,然后单击“查看源代码”,然后找到page_id属性:您必须在此处使用page_id最后一个反斜杠:

fb://page/pageID

例如:

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);
}

答案 14 :(得分:4)

我的回答建立在joaomgcd广泛接受的答案之上。 如果用户已安装但已禁用Facebook(例如,通过使用应用程序隔离),则此方法将不起作用。 Twitter应用程序的意图将被选中,但由于它被禁用,它将无法处理它。

而不是:

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

您可以使用以下内容来决定该怎么做:

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"));

答案 15 :(得分:2)

var app = angular.module('app', []);
app.controller('Controller', function() {
    this.ip = "";

    $("#getIP").on("click", () => {
        $.get("https://api.ipify.org/?format=json", (response) => {
            this.ip = response.ip;
        });
    });
});

答案 16 :(得分:2)

2018年7月起,无论是否在所有设备上都使用Facebook应用,此功能都可以完美运行。

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();
    }
}

private String getFacebookPageURL() {
    String FACEBOOK_URL = "https://www.facebook.com/Yourpage-1548219792xxxxxx/";
    String facebookurl = null;

    try {
        PackageManager packageManager = 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/1548219792xxxxxx";
                }
            } else {
                facebookurl = FACEBOOK_URL;
            }
        } else {
            facebookurl = FACEBOOK_URL;
        }
    } catch (Exception e) {
        facebookurl = FACEBOOK_URL;
    }
    return facebookurl;
}

答案 17 :(得分:1)

要从您的应用启动 facebook页面,请让urlString =“ fb:// page / your_fb_page_id”

要启动 facebook Messenger ,请让urlString =“ fb-messenger:// user / your_fb_page_id”

FB页面ID通常是数字。要获取它,请转到Find My FB ID输入您的个人资料网址,类似www.facebook.com/edgedevstudio,然后单击“查找数字ID”。

Voila,您现在有了fb数字ID。用生成的数字ID替换“ your_fb_page_id”

 val intent = Intent(Intent.ACTION_VIEW, Uri.parse(urlString))
 if (intent.resolveActivity(packageManager) != null) //check if app is available to handle the implicit intent
 startActivity(intent)

答案 18 :(得分:1)

fun getOpenFacebookIntent(context: Context, url: String) {
    return try {
        context.packageManager.getPackageInfo("com.facebook.katana", 0)
        context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/$url/")))
    } catch (e: Exception) {
        context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
    }
}

答案 19 :(得分:1)

声明常量

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

声明方法

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;
        }
    }

通话功能

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

答案 20 :(得分:1)

首先,您需要强烈检查是否安装了任何 Facebook 应用(默认应用、Facebook lite 等) -< /p>

 public static String isFacebookAppInstalled(Context context){
    
            if(context!=null) {
                PackageManager pm=context.getPackageManager();
                ApplicationInfo applicationInfo;
    
                //First check that if the main app of facebook is installed or not
                try {
                    applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
                    return applicationInfo.enabled?"com.facebook.katana":"";
                } catch (Exception ignored) {
                }
    
                //Then check that if the facebook lite is installed or not
                try {
                    applicationInfo = pm.getApplicationInfo("com.facebook.lite", 0);
                    return applicationInfo.enabled?"com.facebook.lite":"";
                } catch (Exception ignored) {
                }
    
                //Then check the other facebook app using different package name is installed or not
                try {
                    applicationInfo = pm.getApplicationInfo("com.facebook.android", 0);
                    return applicationInfo.enabled?"com.facebook.android":"";
                } catch (Exception ignored) {
                }
    
                try {
                    applicationInfo = pm.getApplicationInfo("com.example.facebook", 0);
                    return applicationInfo.enabled?"com.example.facebook":"";
                } catch (Exception ignored) {
                }
            }
            return "";
        }

然后你需要像这样打开facebook应用程序-

            Uri uri = Uri.parse(yourURL);
            if (!TextUtils.isEmpty(isFacebookAppInstalled(context))) {
                uri = Uri.parse("fb://facewebmodal/f?href=" + yourURL);

                Intent intent = context.getPackageManager().getLaunchIntentForPackage(isFacebookAppInstalled(context));
                if (intent != null) {
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(uri);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
                else {
                    Intent intentForOtherApp = new Intent(Intent.ACTION_VIEW, uri);
                    context.startActivity(intentForOtherApp);
                }
            }

答案 21 :(得分:0)

我创建了一个方法来打开Facebook页面进入Facebook应用程序,如果应用程序不存在然后以chrome打开

    String socailLink="https://www.facebook.com/kfc";
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String facebookUrl = Utils.getFacebookUrl(getActivity(), socailLink);
    if (facebookUrl == null || facebookUrl.length() == 0) {
        Log.d("facebook Url", " is coming as " + facebookUrl);
        return;
    }
    intent.setData(Uri.parse(facebookUrl));
    startActivity(intent);

Utils.class 添加这些方法

public static String getFacebookUrl(FragmentActivity activity, String facebook_url) {
    if (activity == null || activity.isFinishing()) return null;

    PackageManager packageManager = activity.getPackageManager();
    try {
        int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
        if (versionCode >= 3002850) { //newer versions of fb app
            Log.d("facebook api", "new");
            return "fb://facewebmodal/f?href=" + facebook_url;
        } else { //older versions of fb app
            Log.d("facebook api", "old");
            return "fb://page/" + splitUrl(activity, facebook_url);
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("facebook api", "exception");
        return facebook_url; //normal web url
    }
}

和这个

 /***
 * this method used to get the facebook profile name only , this method split domain into two part index 0 contains https://www.facebook.com and index 1 contains after / part
 * @param context contain context
 * @param url contains facebook url like https://www.facebook.com/kfc
 * @return if it successfully split then return "kfc"
 *
 * if exception in splitting then return "https://www.facebook.com/kfc"
 *
 */
 public static String splitUrl(Context context, String url) {
    if (context == null) return null;
    Log.d("Split string: ", url + " ");
    try {
        String splittedUrl[] = url.split(".com/");
        Log.d("Split string: ", splittedUrl[1] + " ");
        return splittedUrl.length == 2 ? splittedUrl[1] : url;
    } catch (Exception ex) {
        return url;
    }
}

答案 22 :(得分:0)

在2018年10月回答此问题。工作代码是使用pageID的代码。我刚刚测试了它,并且功能正常。

public static void openUrl(Context ctx, String url){
    Uri uri = Uri.parse(url);
    if (url.contains(("facebook"))){
        try {
            ApplicationInfo applicationInfo = ctx.getPackageManager().getApplicationInfo("com.facebook.katana", 0);
            if (applicationInfo.enabled) {
                uri = Uri.parse("fb://page/<page_id>");
                openURI(ctx, uri);
                return;
            }
        } catch (PackageManager.NameNotFoundException ignored) {
            openURI(ctx, uri);
            return;
        }
    }

答案 23 :(得分:0)

我在webview中使用片段在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;
                }

});

和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;
    }

答案 24 :(得分:0)

在不使用facebook sdk的情况下打开按钮单击事件上的fb

 Intent FBIntent = new Intent(Intent.ACTION_SEND);
    FBIntent.setType("text/plain");
    FBIntent.setPackage("com.facebook.katana");
    FBIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
    try {
        context.startActivity(FBIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "Facebook have not been installed.", Toast.LENGTH_SHORT).show( );
    }

答案 25 :(得分:0)

1-要获取您的ID,请转到您的图片资料,然后点击右键并获取副本链接地址。

        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")));
            }
        }
    });

答案 26 :(得分:0)

查看我打开 Facebook 特定主页的代码:

//ID initialization
ImageView facebook = findViewById(R.id.facebookID);

facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String facebookId = "fb://page/327031464582675";
            String urlPage = "http://www.facebook.com/MDSaziburRahmanBD";

            try {
               startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));
            }catch (Exception e){
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
            }
        }
    });

答案 27 :(得分:-1)

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)));
    }

答案 28 :(得分:-2)

您可以按下按钮打开Facebook应用程序,如下所示: -

@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);
    }
}