You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何让用户从已安装应用列表选择并启动应用

Android 实现已安装应用选择并启动的方案

要实现类似打开PDF时的应用选择器效果,核心是利用Android系统自带的Intent.createChooser()方法,以下是具体实现步骤:

1. 获取已安装应用的启动Intent列表

首先通过PackageManager获取设备上所有已安装应用的信息,并提取每个应用的启动Intent(即打开应用主界面的Intent):

PackageManager packageManager = getPackageManager();
List<ApplicationInfo> installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
List<Intent> launchIntents = new ArrayList<>();

for (ApplicationInfo appInfo : installedApps) {
    // 获取应用的启动Intent
    Intent launchIntent = packageManager.getLaunchIntentForPackage(appInfo.packageName);
    if (launchIntent != null) {
        // 可选:过滤系统应用,只保留第三方应用
        if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
            launchIntents.add(launchIntent);
        }
    }
}

2. 创建并启动系统选择器

将收集到的启动Intent传入Intent.createChooser(),系统会自动弹出选择器供用户选择应用:

if (!launchIntents.isEmpty()) {
    // 第一个参数作为默认选项,第二个参数是选择器标题
    Intent chooserIntent = Intent.createChooser(launchIntents.get(0), "选择要启动的应用");
    // 添加所有可选的启动Intent到选择器中
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, launchIntents.toArray(new Intent[0]));
    
    // 启动选择器
    startActivity(chooserIntent);
}

3. Android 11+ 权限适配

从Android 11开始,系统限制了应用对已安装应用列表的访问权限,需要根据需求进行适配:

  • 如果只需要访问特定应用,在AndroidManifest.xml中添加<queries>标签声明目标应用或Intent过滤规则:
    <queries>
        <!-- 声明需要访问的单个应用包名 -->
        <package android:name="com.example.targetapp" />
        <!-- 或者声明所有可启动的应用(匹配LAUNCHER分类) -->
        <intent>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent>
    </queries>
    
  • 如果需要访问所有已安装应用,需要申请QUERY_ALL_PACKAGES权限:
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:ignore="QueryAllPackagesPermission" />
    

内容的提问来源于stack exchange,提问作者tomography

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 03:31:22