如何检查Facebook是否安装了Android

时间:2011-07-15 18:01:44

标签: android facebook package

我正在修改我的应用程序,以便能够捕获用户是否尝试在未安装Facebook应用程序的情况下发布(SSO需要)。这是我正在使用的代码:

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

问题是,它总是出现错误。根据问题here,我需要请求相应的权限,但我不知道我需要请求哪些权限。

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

9 个答案:

答案 0 :(得分:99)

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

答案 1 :(得分:5)

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

答案 2 :(得分:2)

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

答案 3 :(得分:1)

将这个功能写在实用工具或任何适合你的地方。这个功能将帮助你检查任何安装的应用程序。我自己说它在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);
                }

<强>享受

答案 4 :(得分:0)

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

这段代码对我有用

答案 5 :(得分:0)

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

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

答案 6 :(得分: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;
            }

答案 7 :(得分:0)

您可以检查是否安装了任何 Facebook 应用程序的所有 Facebook 应用程序 -

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

答案 8 :(得分:-1)

myWebView.setWebViewClient(new 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;
        }





    });