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