【问题标题】:How to open facebook profile/page from ImageButton (Android)如何从 ImageButton (Android) 打开 facebook 个人资料/页面
【发布时间】:2014-11-08 17:31:56
【问题描述】:

我是安卓开发新手

我正在为 Facebook 个人资料/页面开发一个带有 imagebutton 的新应用

我试过这段代码,但这段代码在浏览器中打开了 facebook

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

                   }
             }); 
          }
      }

我的问题是如何从 FB 应用程序中的 imagebutton(如果已安装)打开 facebook 个人资料/页面,如果没有,则在浏览器中打开它

我也检查了这个

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

但在浏览器中打开的是同一个 facebook 页面

然后我尝试使用“com.facebook.katana”,但我不知道该怎么做

【问题讨论】:

标签: android facebook onclicklistener imagebutton


【解决方案1】:

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

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

    <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 个人资料页面。

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 应用程序,您只需:

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

** 编辑 **

这就是您可以在活动中调用上述方法的方式。就是这样!

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

【讨论】:

  • 我刚刚写了代码给你一个想法,它是如何工作的。为了在没有任何编译器错误的情况下正常工作,我的假设是它会在必要的地方进行相应的调整。你能告诉我什么样的错误吗?
  • 我知道谢谢我很感激,但我很擅长 java,所以我不知道我可以把方法放在哪里
  • 更新了我的答案以显示它是如何工作的。如果它解决了您的问题,请接受作为帮助社区的答案。
  • 我也做了同样的事,但我没有做到 } 这就是为什么哈哈这么傻
【解决方案2】:

对于 Facebook 个人资料:

//ID initialization
ImageView facebook = findViewById(R.id.facebookID);

//OnClickListener
facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=https://www.facebook.com/techsajib"));
                startActivity(intent);
            } catch(Exception e) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/techsajib")));
            }
        }
    });

对于 Facebook 页面:

 //ID initialization
ImageView facebook = findViewById(R.id.facebookID);

//OnClickListener
facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String facebookId = "fb://page/327031464582675";
            String urlPage = "http://www.facebook.com/MDSaziburRahmanBD";

            try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));
            }catch (Exception e){
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage )));
            }
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多