Android 判断是否安装某个应用并协议跳转

1.代码中判断,通过获取应用列表对比判断:

private boolean checkAppInstalled(Context context, String pkgName) {
        if (pkgName == null || pkgName.isEmpty()) {
            return false;
        }
        final PackageManager packageManager = context.getPackageManager();
        List info = packageManager.getInstalledPackages(0);
        if (info == null || info.isEmpty())
            return false;
        for (int i = 0; i < info.size(); i++) {
            if (pkgName.equals(info.get(i).packageName)) {
                return true;
            }
        }
        return false;
    }

2.Scheme协议跳转

findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean b = checkAppInstalled(DemoActivity.this, "packageNamexxx");
            if (b) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("Scheme协议xxx"));
                startActivity(intent);
            } else {
                Toast.makeText(DemoActivity.this, "未安装此应用", Toast.LENGTH_SHORT).show();
            }

        }
    });

你可能感兴趣的:(工具类)