public static Intent getOpenFacebookIntent(Context context) {
try {
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id_here>"));
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name_here>"));
}
}
This will open the Facebook app if the user has it installed. Otherwise, it will open Facebook in the browser.
EDIT: since version 11.0.0.11.23 (3002850) Facebook App do not support this way anymore, there's another way, check the response below from Jared Rummler.
public final void launchFacebook() {
final String urlFb = "fb://page/"+yourpageid;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlFb));
// If a Facebook app is installed, use it. Otherwise, launch
// a browser
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() == 0) {
final String urlBrowser = "https://www.facebook.com/"+pageid;
intent.setData(Uri.parse(urlBrowser));
}
startActivity(intent);
}
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
您可以使用以下方法来决定应该做什么:
PackageInfo info = context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
if(info.applicationInfo.enabled)
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
else
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/620681997952698"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startNewActivity("com.facebook.katana");
}
});
}
public void startNewActivity( String packageName)
{
Intent intent = MainActivity.this.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent != null)
{
// we found the activity
// now start the activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
// bring user to the market
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id="+packageName));
startActivity(intent);
}
}
/**
* <p>Intent to open the official Facebook app. If the Facebook app is not installed then the
* default web browser will be used.</p>
*
* <p>Example usage:</p>
*
* {@code newFacebookIntent(ctx.getPackageManager(), "https://www.facebook.com/JRummyApps");}
*
* @param pm
* The {@link PackageManager}. You can find this class through {@link
* Context#getPackageManager()}.
* @param url
* The full URL to the Facebook page or profile.
* @return An intent that will open the Facebook page/profile.
*/
public static Intent newFacebookIntent(PackageManager pm, String url) {
Uri uri = Uri.parse(url);
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
if (applicationInfo.enabled) {
// http://stackoverflow.com/a/24547437/1048340
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
}
} catch (PackageManager.NameNotFoundException ignored) {
}
return new Intent(Intent.ACTION_VIEW, uri);
}
private void openFacebookApp() {
String facebookUrl = "www.facebook.com/XXXXXXXXXX";
String facebookID = "XXXXXXXXX";
try {
int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
if(!facebookID.isEmpty()) {
// open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
Uri uri = Uri.parse("fb://page/" + facebookID);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
// open Facebook app using facebook url
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} catch (PackageManager.NameNotFoundException e) {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
}
public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
public static String FACEBOOK_PAGE_ID = "YourPageName";
//method to get the right URL to use in the intent
public String getFacebookPageURL(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) { //newer versions of fb app
return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
} else { //older versions of fb app
return "fb://page/" + FACEBOOK_PAGE_ID;
}
} catch (PackageManager.NameNotFoundException e) {
return FACEBOOK_URL; //normal web url
}
}
// e.g. if your URL is https://www.facebook.com/EXAMPLE_PAGE, you should
// put EXAMPLE_PAGE at the end of this URL, after the ?
String YourPageURL = "https://www.facebook.com/n/?YOUR_PAGE_NAME";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(YourPageURL));
startActivity(browserIntent);
/***
* this method used to get the facebook profile name only , this method split domain into two part index 0 contains https://www.facebook.com and index 1 contains after / part
* @param context contain context
* @param url contains facebook url like https://www.facebook.com/kfc
* @return if it successfully split then return "kfc"
*
* if exception in splitting then return "https://www.facebook.com/kfc"
*
*/
public static String splitUrl(Context context, String url) {
if (context == null) return null;
Log.d("Split string: ", url + " ");
try {
String splittedUrl[] = url.split(".com/");
Log.d("Split string: ", splittedUrl[1] + " ");
return splittedUrl.length == 2 ? splittedUrl[1] : url;
} catch (Exception ex) {
return url;
}
}
现在,您已经得到了 fb 数字 ID。用生成的 Numeric ID 替换“ your _ fb _ page _ ID”
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(urlString))
if (intent.resolveActivity(packageManager) != null) //check if app is available to handle the implicit intent
startActivity(intent)
Intent FBIntent = new Intent(Intent.ACTION_SEND);
FBIntent.setType("text/plain");
FBIntent.setPackage("com.facebook.katana");
FBIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
try {
context.startActivity(FBIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Facebook have not been installed.", Toast.LENGTH_SHORT).show( );
}
public static String isFacebookAppInstalled(Context context){
if(context!=null) {
PackageManager pm=context.getPackageManager();
ApplicationInfo applicationInfo;
//First check that if the main app of facebook is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
return applicationInfo.enabled?"com.facebook.katana":"";
} catch (Exception ignored) {
}
//Then check that if the facebook lite is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.lite", 0);
return applicationInfo.enabled?"com.facebook.lite":"";
} catch (Exception ignored) {
}
//Then check the other facebook app using different package name is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.android", 0);
return applicationInfo.enabled?"com.facebook.android":"";
} catch (Exception ignored) {
}
try {
applicationInfo = pm.getApplicationInfo("com.example.facebook", 0);
return applicationInfo.enabled?"com.example.facebook":"";
} catch (Exception ignored) {
}
}
return "";
}
然后启动应用程序-
if (!TextUtils.isEmpty(isFacebookAppInstalled(context))) {
/* Facebook App is installed,So launch it.
It will return you installed facebook app's package
name which will be useful to launch the app */
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + yourURL);
Intent intent = context.getPackageManager().getLaunchIntentForPackage(isFacebookAppInstalled(context);
if (intent != null) {
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
else {
Intent intentForOtherApp = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intentForOtherApp);
}
}
private void openFacebookApp() {
String facebookUrl = "www.facebook.com/XXXXXXXXXX";
String facebookID = "XXXXXXXXX";
try {
int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
if(!facebookID.isEmpty()) {
// open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
Uri uri = Uri.parse("fb://page/" + facebookID);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
// open Facebook app using facebook url
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} catch (PackageManager.NameNotFoundException e) {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}