首页 > 解决方案 > 如何创建从我的应用到 Facebook 页面的意图?

问题描述

这似乎是一个已成定局的问题,我认为我已经可以很容易地找到答案,但我找到的答案已经很老了,而且它们不起作用。如何创建从我的应用到 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

标签: androidfacebookandroid-intentfacebook-page

解决方案


您可以使用以下代码重定向到特定用户您的 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);
    }

希望这可以帮助您快乐编码!


推荐阅读