【问题标题】:How do I start another application (downloaded or preinstalled) from an activity?如何从活动中启动另一个应用程序(下载或预安装)?
【发布时间】:2010-04-18 22:58:27
【问题描述】:

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

我已经尝试使用 Intents 进行 ACTION_PICK,但这似乎遗漏了已下载的应用程序,并且其中包含一堆垃圾。

谢谢

【问题讨论】:

  • 不完全是重复的,但您需要的信息是here

标签: android android-activity


【解决方案1】:
// 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)

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

【讨论】:

  • 我试过了,但它没有显示所有已安装的应用程序,例如已下载的应用程序。
【解决方案2】:
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);

        }
    }

这将帮助您加载所有下载的应用程序。

【讨论】:

  • @2Real 只需检查上面的代码,我已经使用这个来获取我设备中的所有包,希望这个有用。
最近更新 更多