Intent to open profile with the facebook app using Page ID, Android?

7

I am trying to open a facebook profile with a button, but I have tried and always open with the browser and not with the facebook app, someone can help me please, so I have it at this time.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.facebook);
    //FACEBOOK
    Uri uri = Uri.parse("https://www.facebook.com/uniagustiniana?ref=hl/");
    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
    try{
        startActivity(intent);
    }catch(Exception e){
        e.printStackTrace();
    }finally {
        finish();
    }

    }
    
asked by Ivan Alfredo 10.05.2017 в 15:56
source

2 answers

5

For this you need the "Facebook page id", which is obtained in this way:

  • From the page go to "About"
  • Go to "More Info"
  • There will be the "Facebook Page ID"

To open the application, it is done like this:

 String facebookId = "fb://page/<Facebook Page ID>"; //*Los signos < y > no van!
 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));

I recommend you validate the case in which the installed application does not exist. In this case, you will open the web page if you can not find the application installed:

String facebookId = "fb://page/<Facebook Page ID>";
String urlPage = "http://www.facebook.com/mypage";

     try {
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));
        } catch (Exception e) {
         Log.e(TAG, "Aplicación no instalada.");
         //Abre url de pagina.
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    
answered by 10.05.2017 / 17:53
source
1

This could work:

public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
public static String FACEBOOK_PAGE_ID = "YourPageName";

//método que obtiene la verdadera URL
public String getFacebookPageURL(Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {
        int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
        if (versionCode >= 3002850) { //versiones nuevas de facebook
            return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
        } else { //versiones antiguas de fb
            return "fb://page/" + FACEBOOK_PAGE_ID;
        }
    } catch (PackageManager.NameNotFoundException e) {
        return FACEBOOK_URL; //normal web url
    }
}

The method returns the correct facebook URL whether you have the app installed or not.

After the Intent is launched

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

I hope it serves you

    
answered by 10.05.2017 в 16:19