如何从的ImageButton打开Facebook的个人主页/页(Android版) [英] How to open facebook profile/page from ImageButton (Android)

查看:1077
本文介绍了如何从的ImageButton打开Facebook的个人主页/页(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的android开发

I'm new to android development

我发展与ImageButton的新应用Facebook的个人主页/页

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

我想这code,但这个code打开浏览器facebook的

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

                   }
             }); 
          }
      }

我的提问是如何打开Facebook的个人主页/页从的ImageButton在FB应用(如果已安装的话),如果没有,那么在浏览器中打开

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

我也检查了这

<一个href=\"http://stackoverflow.com/questions/22317513/how-to-link-the-image-button-to-a-facebook-page-in-android\">how在图像按钮,在Android的链接到Facebook页面

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

但其同样的Facebook页面在浏览器中打开

but its the same facebook page opens in the browser

然后我试着用com.facebook.katana,但我不知道该怎么做。

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

推荐答案

Facebook的Andr​​oid应用程序不支持以来的 1.9.11 版本这个动作隐含的意图机制。 Facebook目前使用相同的iPhone计划机制 FB:// Facebook的:// 来处理所提到的所有动作<一个HREF =htt​​p://wiki.akosma.com/IPhone_URL_Schemes相对=nofollow>这里。

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

这篇关于如何从的ImageButton打开Facebook的个人主页/页(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆