如何检查Facebook是否安装了Android

我正在修改我的应用程序,以便能够捕捉如果用户试图发布没有安装facebook应用程序(SSO所需)。 这是我正在使用的代码:

try{ ApplicationInfo info = getPackageManager(). getApplicationInfo("com.facebook.android", 0 ); return true; } catch( PackageManager.NameNotFoundException e ){ return false; } 

问题是,它总是捕捉一个错误。 根据这里的问题,我需要请求适当的权限,但我不知道我需要什么权限请求。

我的问题是一个许可或其他什么?

com.facebook.android是Facebook SDK的包名称。 Facebook的应用程序是com.facebook.katana。

要检查安装在Android上的任何应用程序或不使用此方法:

 public static boolean isPackageExisted(Context c, String targetPackage) { PackageManager pm = c.getPackageManager(); try { PackageInfo info = pm.getPackageInfo(targetPackage, PackageManager.GET_META_DATA); } catch (NameNotFoundException e) { return false; } return true; } 

在你的情况下使用任何的包

  • com.facebook.orca
  • com.facebook.katana
  • com.example.facebook
  • com.facebook.android

boolean hasPackage = isPackageExisted(MainActivity.this,“com.facebook.katana”)

希望它会帮助你。

最好的办法是select包名称,包括com.facebook,但无论如何,您可以使用下列软件包:

  • com.facebook.orca
  • com.facebook.katana
  • com.example.facebook
  • com.facebook.android

在“实用程序”或任何适合你的地方写这个函数。这个函数将帮助你检查是否安装了应用程序。我自己说,它是在Utilities.java

 public static boolean isAppInstalled(Context context, String packageName) { try { context.getPackageManager().getApplicationInfo(packageName, 0); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } 

然后,从任何地方调用这个function。 例如检查Facebook应用程序

 if(Utilities.isAppInstalled(getApplicationContext(), "com.facebook.katana")) { // Do something }else { Intent i = new Intent(android.content.Intent.ACTION_VIEW); i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.katana")); startActivity(i); } 

请享用

  if (isAppInstalled()) { Toast.makeText(getApplicationContext(), "facebook app already installed", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "facebook app not installing", Toast.LENGTH_SHORT).show(); } public boolean isAppInstalled() { try { getApplicationContext().getPackageManager().getApplicationInfo("com.facebook.katana", 0); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } 
 Intent i = new Intent(android.content.Intent.ACTION_VIEW); i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.katana")); startActivity(i); 

这段代码为我工作