如何从活动启动另一个应用程序(下载或预安装)?

时间:2010-04-18 22:58:27

标签: android android-activity

基本上,我想获得所有已安装应用的列表,然后选择一个从活动中运行。

我已尝试使用Intents进行ACTION_PICK,但这似乎忽略了已下载的应用程序,并且其中包含大量垃圾。

由于

2 个答案:

答案 0 :(得分:8)

// to get the list of apps you can launch
Intent intent = new Intent(ACTION_MAIN);
intent.addCategory(CATEGORY_LAUNCHER);
List<ResolveInfo> infos = getPackageManager().queryIntentActivities(intent, 0);

// resolveInfo.activityInfo.packageName = packageName
// resolveInfo.activityInfo.name = className
// reusing that intent
intent.setClassName(packageName, className);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent)

希望这足以帮助你弄清楚。

答案 1 :(得分:0)

final File favFile = new File(Environment.getRootDirectory(), DEFAULT_FAVORITES_PATH);
            try {
                favReader = new FileReader(favFile);
            } catch (FileNotFoundException e) {
                Log.e(LOG_TAG, "Couldn't find or open favorites file " + favFile);
                return;
            }//gives the path for downloaded apps in directory







 private void loadApplications(boolean isLaunching) {
        if (isLaunching && mApplications != null) {
            return;
        }

        PackageManager manager = getPackageManager();

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
        Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));

        if (apps != null) {
            final int count = apps.size();

            if (mApplications == null) {
                mApplications = new ArrayList<ApplicationInfo>(count);
            }
            mApplications.clear();

            for (int i = 0; i < count; i++) {
                  ApplicationInfo application = new ApplicationInfo();
                ResolveInfo info = apps.get(DEFAULT_KEYS_SEARCH_LOCAL);

                application.title = info.loadLabel(manager);
                application.setActivity(new ComponentName(
                        info.activityInfo.applicationInfo.packageName,
                        info.activityInfo.name),
                        Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                application.icon = info.activityInfo.loadIcon(manager);

                mApplications.add(application);

        }
    }

这将有助于您加载下载的所有应用。