如何以编程方式检查我的设备中是否安装了facebook应用程序

时间:2014-09-09 07:49:00

标签: android

我创建了一个Android照片应用程序,并在其中我使用意图在Facebook,Twitter,Instagram上添加图像共享的功能但是当我分享图像时,如果未在设备上安装facebook,twitter,instagram应用程序,应用程序崩溃。

1 个答案:

答案 0 :(得分:6)

请参阅this问题。它会为您提供所需内容。只需使用 com.facebook.android com.facebook.katana 替换包名称

代码:

public class Example extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Put the package name here...
        boolean installed  =   appInstalledOrNot("com.Ch.Example.pack");  
        if(installed) {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.facebook.katana");//if this name does not work provide the other one
            startActivity(LaunchIntent);

            System.out.println("App already installed on your phone");        
        }
        else {
            System.out.println("App is not installed on your phone");
        }
    }

    private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed ;
    }
}