android 通过 包名启动Activity及intent.addCategory(Intent.CATEGORY_LAUNCHER);遗留问题

最近因为项目需要,不让app在应用列表显示,只能用自己的启动器启动。

private void RunApp(String packageName) {
        PackageInfo pi;
        try {
            pi = getPackageManager().getPackageInfo(packageName, 0);
            Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
            // resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            resolveIntent.setPackage(pi.packageName);
            PackageManager pManager = getPackageManager();
            List apps = pManager.queryIntentActivities(
                    resolveIntent, 0);

            ResolveInfo ri = apps.iterator().next();
            if (ri != null) {
                packageName = ri.activityInfo.packageName;
                String className = ri.activityInfo.name;

                Intent intent = new Intent(Intent.ACTION_MAIN);
                // intent.addCategory(Intent.CATEGORY_LAUNCHER);

                ComponentName cn = new ComponentName(packageName, className);

                intent.setComponent(cn);
                startActivity(intent);
            }
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

问题是:当intent.addCategory(Intent.CATEGORY_LAUNCHER);注释去除时,部分应用点击后会导致程序异常退出。更确切的说是找不到apps。初步判断是因为这些应用没有在Intent-filter中设置。这些应用是无法在桌面列表图标中显示的。所以,如果加上intent.addCategory(Intent.CATEGORY_LAUNCHER);因为这些应用没有这一属性,所以导致点击失败(即无法获取到对应的apps)。


你可能感兴趣的:(android)