Android检测是否安装了指定应用

检测是否安装了指定应用的方法如下:

private boolean isAvilible( Context context, String packageName )

    {

        final PackageManager packageManager = context.getPackageManager();

        // 获取所有已安装程序的包信息

        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);

        for ( int i = 0; i < pinfo.size(); i++ )

        {

            if(pinfo.get(i).packageName.equalsIgnoreCase(packageName))

                return true;

        }

        return false;

    }

 

然后检测并启动或安装:

if(isAvilible(MainActivity.this, "com.tencent.mm")){ 

           Intent i = new Intent(); 

           ComponentName cn = new ComponentName("com.tencent.mm", 

                            "com.tencent.mm.WeiXinActivity"); 

           i.setComponent(cn); 

           startActivityForResult(i, RESULT_OK);    

   } 

    //未安装,跳转至market下载该程序 

    else { 

                    Uri uri = Uri.parse("market://details?id=com.tencent.mm");

                    // 直接从指定网址下载

                    // Uri uri = Uri.parse("http://dldir1.qq.com/foxmail/weixin521android400.apk");

                    Intent it = new Intent(Intent.ACTION_VIEW, uri); 

                    startActivity(it); 

       }

你可能感兴趣的:(android)