2011-07-15 279 views
58

我正在修改我的应用程序,以便能够捕捉用户是否尝试发布而未安装Facebook应用程序(SSO需要)。这里是我使用的代码:如何检查Facebook是否安装Android

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

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

是我的问题一个权限或其他?

回答

91

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

+0

如果您有兴趣以便捷的方式检查设备上其他应用的软件包名称,我发现这个应用很有用:https://play.google.com/store/apps/details?id=com。 electricsheep.asi&hl = en_GB – hmac

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

此代码为我工作

+0

回答与问题无关。他在问别的东西。 – AndroidMechanic

+0

请在回答问题之前仔细阅读。干杯 –

0

最好的办法是选择包括com.facebook软件包的名称,但无论如何你可以使用下面的包:

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

写出you.This意志功能将帮助您检查已安装任何应用程序或not.let我说我自己是在Utilities.java

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

然后在工具的功能或任何诉讼,从任何地方调用此功能。对于如检查的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); 
       } 

享受

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

要检查的任何应用程序安装在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

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

希望它能帮助你。