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

落爺英雄遲暮 提交于 2019-12-25 14:58:30

问题


I'm new to android development

I developing a new app with imagebutton to facebook profile/page

I tried this code but this code open the facebook in the browser

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) 
              { 
                   Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(
                   "http://www.facebook.com/sarmad.waleed.7"));
                    startActivity(browserIntent);

                   }
             }); 
          }
      }

My question is how open the facebook profile/page from imagebutton in FB app (if it installed) and if not then open it in the browser

I also check this

how to link the image button to a facebook page in android

but its the same facebook page opens in the browser

then I tried with "com.facebook.katana" but I don't know how to do it


回答1:


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


来源:https://stackoverflow.com/questions/26820020/how-to-open-facebook-profile-page-from-imagebutton-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!