如何从我的应用程序创建到Facebook页面的意图?

时间:2018-09-24 12:53:24

标签: android facebook android-intent facebook-page

这似乎是一个已成定局的问题,我想我已经可以轻松找到答案了, 但是我发现的答案很旧,无法解决。 如何从我的应用程序创建到Facebook页面的意图? 我想如果有打开的Facebook应用程序,否则浏览器

到目前为止,最好的解决方案是Rishav Singla:

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

使用:

startActivity(getOpenFacebookIntent(getApplicationContext()));

...在attesa di altre soluzioni中

4 个答案:

答案 0 :(得分: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>")); // your id
} catch (Exception e) {
 return new Intent(Intent.ACTION_VIEW, 
Uri.parse("https://www.facebook.com/<user_name_here>"));
}
}

答案 1 :(得分:1)

您可以使用以下代码重定向到特定用户 您的Facebook应用程序将在后台运行,因此要打开它,您必须运行两次此代码(这是一种奇怪的行为:()

try
    {
        Intent followIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<your profile_id>"));
        startActivity(followIntent);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run() {
                Intent followIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<your profile_id>"));
                startActivity(followIntent);
            }
        }, 1000 * 2);

    }
    catch (Exception e)
    {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name>")));
        String errorMessage = (e.getMessage()==null)?"Message is empty":e.getMessage();
        Log.e("Unlock_ScreenActivity:FacebookAppNotFound" ,errorMessage);
    }

希望这对您有帮助 编码愉快!

答案 2 :(得分:0)

使用URL到Facebook在浏览器中打开

    String url = "https://www.facebook.com/ZambianWatchdog/";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

答案 3 :(得分:0)

public Intent getIntent() {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/{{page}}"));
    if (intent.resolveActivity(getPackageManager()) != null) {
        return intent;
    }
    return null;
}