Luigi Massimini
Luigi Massimini

Reputation: 59

Choose between facebook app and browser?

I created a preference and I want back to my facebook page. I would like to click a user has the option to choose between the facebook app and the browser. Now part of the browser directly. The code is:

Facebook = (Preference) this.findPreference("facebook");
Facebook.setOnPreferenceClickListener( new OnPreferenceClickListener() {
    public boolean onPreferenceClick (Preference preference) {

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/MyPageName"));
startActivity(browserIntent);
return false;
}
    });

Upvotes: 1

Views: 249

Answers (1)

Siddharth Lele
Siddharth Lele

Reputation: 27748

This is how I check if Google Maps is installed. Modified for checking the Facebook app status.

// CHECK IF THE FACEBOOK APP IS INSTALLED
PackageManager pkManager = activity.getPackageManager();
try {
    PackageInfo pkgInfo = pkManager.getPackageInfo("com.facebook.katana", 0);
    String getPkgInfo = pkgInfo.toString();

    if (getPkgInfo.equals("com.facebook.katana"))   {
        // OPEN THE PAGE IN THE FACEBOOK APP
        String strPage = "fb://profile/" + YOUR_PAGE_ID;

        // CHECK IF THE "fb://profile/" IS CORRECT | REPLACE WITH "fb://page/"
        // NOT ENTIRELY SURE WHICH ONE WORKS

        Intent showPageInFB = new Intent(Intent.ACTION_VIEW, Uri.parse(strPage)); 
        startActivity(showPageInFB );
    } else {
        // OPEN THE PAGE IN A BROWSER
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/MyPageName"));
        startActivity(browserIntent);
    }
} catch (NameNotFoundException e) {
    e.printStackTrace();
}

Upvotes: 2

Related Questions