且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从ImageButton(Android)打开Facebook个人资料/页面

更新时间:2023-12-02 18:16:34

自从 1.9.11 版本起,Facebook android应用不支持此操作的隐含意图机制。 Facebook现在使用相同的iPhone方案机制 fb:// facebook:// 来处理所提及的所有操作 here

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.

在这里你可以看到他们支持fb和facebook方案。

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>

根据您的要求,此方法将处理这两种情况。首先它将检查是否安装了Facebook应用程序,否则将在浏览器中打开Facebook个人资料页面。

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

为了打开具有用户个人资料的Facebook应用程序,您需要做是:

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

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

** 编辑 **

这是你可以在你的活动中调用上面的方法。就是这样!

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