4

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222

4 Answers4

11

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);
    }
Mayank Saini
  • 3,017
  • 24
  • 25
  • It worked but in new facebook update the link to page is "https://www.facebook.com/"+pageid; this instead of "https://www.facebook.com/pages/"+pageid; – Divyanshu Negi Feb 23 '15 at 05:08
4

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

MaximD
  • 186
  • 2
  • 6
  • This solution won't work any more. You can find the answer in the following link http://stackoverflow.com/a/13107858/1020530 – nheimann1 Nov 01 '12 at 09:32
  • Hey, this is not a good solution since it explicitly uses ProfileTabHostActivity. In future version Facebook could decide to rename that activity or remove it from the app. Then your solution wouldn't work. – Tomasz Nov 22 '13 at 23:54
1

try this code :

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)));
    }
Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38
0

This solution won't work any more. The new version of the facebook app doesn't support anymore those kind of intents. See here the bug report

The new solution is to use the iPhone scheme mechanism (Yes, facebook decided to support the iPhone mechanism in Android instead of the implicit intent mechanism of Android).

So in order to open the facebook app with a user profile all you need to do is:

String facebookScheme = "fb://profile/" + facebookId;
Intent facebookIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme)); 
startActivity(facebookIntent);

If you are looking for other actions you can use the following page for all available actions (/you have to test it though, since I didn't find an official publication of facebook about this)

nheimann1
  • 2,348
  • 2
  • 21
  • 33