Open Facebook Page from Android App

Open Facebook page from Android app?

In Facebook version 11.0.0.11.23 (3002850) fb://profile/ and fb://page/ no longer work. I decompiled the Facebook app and found that you can use fb://facewebmodal/f?href=[YOUR_FACEBOOK_PAGE]. Here is the method I have been using in production:

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

Open Facebook Page in Facebook App (if installed) on Android

"fb://page/ does not work with newer versions of the FB app. You should use fb://facewebmodal/f?href= for newer versions.

This is a full fledged working code currently live in one of my apps:

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

This method will return the correct url for app if installed or web url if app is not installed.

Then start an intent as follows:

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

That's all you need.

How to open profile on Facebook app from Android app

I think You do not declare your context in main body, You have to add,

context=getApplicationContext(); 

or

context=YourActivity.this;

to your body.

This is worked for me.

Button btn;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn);
context=getApplicationContext();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAppInstalled()) {
Toast.makeText(getApplicationContext(), "facebook app already installed", Toast.LENGTH_SHORT).show();
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(context);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);

} else {
Toast.makeText(getApplicationContext(), "facebook app not installing", Toast.LENGTH_SHORT).show();
}



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



public boolean isAppInstalled() {
try {
getApplicationContext().getPackageManager().getApplicationInfo("com.facebook.katana", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

}

Android open facebook page link in facebook app if installed ,

Using this code you can open user profile

public static void opneFacebookProfile(Activity activity, String id) {
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(activity, id);
facebookIntent.setData(Uri.parse(facebookUrl));
activity.startActivity(facebookIntent);
}

How to open facebook profile/page from ImageButton (Android)

Facebook android app don't support implicit intent mechanism for this action since 1.9.11 version. Facebook now use the same iPhone scheme mechanism fb:// or facebook://to handle all actions mentioned here.

And here you can see that they support the fb and facebook scheme.

    <activity android:name="com.facebook.katana.IntentUriHandler">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="facebook" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="fb" />
</intent-filter>
</activity>

As per your requirement, this method will handle both scenarios. First it will check if the facebook app is installed otherwise it will open facebook profile page in browser.

public Intent getFBIntent(Context context, String facebookId) {

try {
// Check if FB app is even installed
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);

String facebookScheme = "fb://profile/" + facebookId;
return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme));
}
catch(Exception e) {

// Cache and Open a url in browser
String facebookProfileUri = "https://www.facebook.com/" + facebookId;
return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookProfileUri));
}

return null;
}

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

Intent facebookIntent = getFBIntent(this, "2347633432");
startActivity(facebookIntent);

** EDIT **

This is how you can call the above method in your activity. That's it!

public class AboutActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);

ImageButton f = (ImageButton)findViewById(R.id.f_logo);
f.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
// Get the intent
Intent intent = getFBIntent(AboutActivity.this, "sarmad.waleed.7");

// Start the activity
if (intent != null)
startActivity(intent);
}
});
}

/**
* Get the facebook intent for the given facebook
* profile id. If the facebook app is installed, then
* it will open the facebook app. Otherwise, it will
* open the facebook profile page in browser.
*
* @return - the facebook intent
*/
private Intent getFBIntent(Context context, String facebookId) {

try {
// Check if FB app is even installed
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);

String facebookScheme = "fb://profile/" + facebookId;
return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme));
}
catch(Exception e) {

// Cache and Open a url in browser
String facebookProfileUri = "https://www.facebook.com/" + facebookId;
return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookProfileUri));
}

return null;
}
}

Open a facebook page from android app?

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



Related Topics



Leave a reply



Submit