如何检查Facebook是否安装了Android

eas*_*ese 61 android facebook package

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

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

问题是,它始终是一个错误.根据这里的问题,我需要请求相应的权限,但我不知道我需要请求什么权限.

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

Tor*_*rid 100

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


N.D*_*oid 6

要检查Android上是否安装了应用,请使用以下方法:

public static boolean isPackageInstalled(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;
}
Run Code Online (Sandbox Code Playgroud)

在您的情况下使用任何这些包:

  • com.facebook.orca
  • com.facebook.katana
  • com.example.facebook
  • com.facebook.android
boolean hasPackage = isPackageInstalled(MainActivity.this, "com.facebook.katana");
Run Code Online (Sandbox Code Playgroud)


Gk *_*mon 6

您可以检查所有 Facebook 应用程序是否安装了任何 Facebook 应用程序。为了支持操作系统级别 11,我们需要添加此AndrodiManifest.xml以避免包名称未找到异常 -

<manifest ...
<queries>
    <package android:name="com.facebook.katana" />
    <package android:name="com.facebook.lite" />
    <package android:name="com.facebook.android" />
    <package android:name="com.example.facebook" />
</queries>
 <application .....
Run Code Online (Sandbox Code Playgroud)

然后将此方法添加到您的代码中 -

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 "";
    }
Run Code Online (Sandbox Code Playgroud)

然后启动应用程序 -

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);
                } 
 }
Run Code Online (Sandbox Code Playgroud)