找出是否安装了应用程序

时间:2013-06-28 20:29:49

标签: android facebook package whatsapp

我认为问题就是这样:找出用户是否在手机上安装了Facebook或Whatsapp的最佳方法是什么?我是否必须检查包装或者最好的方法是什么?

1 个答案:

答案 0 :(得分:12)

回答问题here。您可以使用以下代码来检查包名称

  

com.facebook.android OR 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.facebook.android");  
            if(installed)
            {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.facebook.android");
        startActivity(LaunchIntent);


                      System.out.println("App already installed om your phone");


            }
            else
            {
                System.out.println("App is not installed om 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 ;
    }
    }