Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a facebook page from android app?

How can I start an intent to open a Facebook application on a phone and navigate to the prefered page in Facebook?

I tried:

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

Well, whatever I write to "1234567891", it is always navigating to my page. Always to me and not else.

How could I do this?

like image 695
Adam Varhegyi Avatar asked Dec 04 '22 16:12

Adam Varhegyi


2 Answers

Here is the best and simple way to do it. Just follow the code

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

        // If Facebook application is installed, use that else 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);
    }
like image 119
Mayank Saini Avatar answered Dec 22 '22 21:12

Mayank Saini


I had the exactly same problem, sent the user id but for some reason, my profile always opened instead of the friend's profile.

The problem is that if you pass the String of the Long object that represents the Facebook UID, or even a long primitive type, the intent won't be able to read it later. You need to pass a real Long.

So the complete code is:

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

Ok enjoy and hope this helps :-)

Maxim

like image 41
MaximD Avatar answered Dec 22 '22 20:12

MaximD