简体   繁体   English

如何检查 Facebook 是否安装 Android

[英]How to check if Facebook is installed Android

I am modifying my app to be able to catch if a user tries to publish without having the facebook app installed (required for SSO).我正在修改我的应用程序,以便能够在用户尝试在未安装 facebook 应用程序(SSO 所需)的情况下发布。 Here is the code I am using:这是我正在使用的代码:

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

The problem is, it is always catching an error.问题是,它总是在捕捉错误。 According to the question here , I need to request the appropriate permission but I don't know what permissions I need to request.根据这里的问题,我需要请求适当的权限,但我不知道我需要请求什么权限。

Is my problem a permission one or something else?我的问题是许可问题还是其他问题?

com.facebook.android is the package name for the Facebook SDK. com.facebook.android is the package name for the Facebook SDK. The Facebook app's package is com.facebook.katana . Facebook 应用程序的 package 是com.facebook.katana

To check whether or not an app is installed on Android use this method:要检查 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;
}

In your case use any of these packages:在您的情况下,请使用以下任何软件包:

  • com.facebook.orca com.facebook.orca
  • com.facebook.katana com.facebook.katana
  • com.example.facebook com.example.facebook
  • com.facebook.android com.facebook.android
boolean hasPackage = isPackageInstalled(MainActivity.this, "com.facebook.katana");
 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;
            }
        }

Write the function in Utilities or anywhere suit for you.This will function will help you to check any app installed or not.let me say for myself it is in Utilities.java在实用程序或适合您的任何地方写入 function。这将 function 将帮助您检查是否安装了任何应用程序。让我自己说它在实用程序中。

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

Then, Call this function from anywhere.然后,从任何地方调用这个 function。 for eg to check facebook app例如检查 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);
                }

Enjoy享受

Best Approach is to pick the package name including com.facebook but anyway you may use following packages:最好的方法是选择 package 名称,包括 com.facebook 但无论如何您都可以使用以下软件包:

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

You can check it for all Facebook Apps that any of facebook apps is installed or not -您可以检查是否安装了任何 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 "";
    }

And then launch the app -然后启动应用程序 -

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

this code worked for me这段代码对我有用

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

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





    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM