如何检查Android设备上是否安装了Facebook应用程序?

69

我正在修改我的应用程序,以便在用户尝试发布内容时捕获是否已安装 Facebook 应用程序(SSO 所必需)。这是我正在使用的代码:

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

问题在于,它总是捕获错误。 根据这里的问题,我需要请求适当的权限,但我不知道需要请求哪些权限。

我的问题是权限问题还是其他问题?

9个回答

105

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


1
如果你想方便地检查设备上其他应用程序的包名称,我发现这个应用程序很有帮助:https://play.google.com/store/apps/details?id=com.electricsheep.asi&hl=en_GB - hmac
@hmac链接已经失效 - radus14

12

要检查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;
}

在您的情况下,请使用以下任何一个包:

  • com.facebook.orca
  • com.facebook.katana
  • com.example.facebook
  • com.facebook.android
boolean hasPackage = isPackageInstalled(MainActivity.this, "com.facebook.katana");
  • For Kotlin

      fun isPackageInstalled(packageName: String, context: Context): Boolean {
         return try {
                  val packageManager = context.packageManager
                  packageManager.getPackageInfo(packageName, 0)
                  true
              } catch (e: PackageManager.NameNotFoundException) {
                  false
              }
          }
    

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 .....

然后将此方法添加到您的代码中 -
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);
                } 
 }

将清单添加到查询中是有效的。 - Ucdemir

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

1

请在Utilities或适合您的任何地方编写此函数。这个函数将帮助您检查任何应用程序是否已安装。就我而言,它在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
最佳方法是选择包含com.facebook的包名,但无论如何,您可以使用以下包:

最好的方法是选择包含com.facebook的包名,但无论如何,您可以使用以下包:

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

-1

myWebView.setWebViewClient(new WebViewClient() {

我的WebView设置了一个WebViewClient() { }。
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.e("tag","url override url  = "+ url);

            if( url.startsWith("http:") || url.startsWith("https:") ) {
                return false;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity( intent );

            return true;
        }





    });

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

这段代码对我有效


他在问其他问题,回答与他的问题无关。 - Viral Patel
请在回答问题之前仔细阅读。干杯 - Adnan Bin Mustafa

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

发布没有格式或解释的代码有点糟糕! - Badro Niaimi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接