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

Android Kotlin如何获取用户安装的应用列表?

Android 获取已安装应用列表的新方案

1. 权限配置

  • 在AndroidManifest.xml中添加对应权限:
<!-- 若需要获取所有已安装应用,添加此权限 -->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
    tools:ignore="QueryAllPackagesPermission" />

注:如果仅需访问特定类别应用,可使用<queries>标签替代上述权限,避免申请QUERY_ALL_PACKAGES。例如只获取浏览器类应用:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
</queries>

2. 获取已安装应用信息

使用PackageManager的getInstalledApplications()方法,结合应用信息类获取名称、图标、包名等核心数据:

代码示例(Kotlin)

// 在Activity/Fragment中调用
val packageManager = packageManager
val installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
    .filter {
        // 可选:过滤系统应用,保留第三方应用
        packageManager.getApplicationInfo(it.packageName, 0).flags and ApplicationInfo.FLAG_SYSTEM == 0
    }
    .map { appInfo ->
        // 封装为自定义数据类
        AppInfo(
            appName = packageManager.getApplicationLabel(appInfo).toString(),
            packageName = appInfo.packageName,
            appIcon = packageManager.getApplicationIcon(appInfo)
        )
    }

// 自定义数据类,用于存储应用信息
data class AppInfo(
    val appName: String,
    val packageName: String,
    val appIcon: Drawable
)

3. ListView 展示应用列表

步骤1:创建列表项布局(item_app.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/iv_app_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:marginEnd="16dp"/>

    <TextView
        android:id="@+id/tv_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"/>
</LinearLayout>

步骤2:自定义BaseAdapter

class AppListAdapter(private val context: Context, private val appList: List<AppInfo>) : BaseAdapter() {
    override fun getCount(): Int = appList.size

    override fun getItem(position: Int): Any = appList[position]

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.item_app, parent, false)
        val currentApp = appList[position]
        view.findViewById<ImageView>(R.id.iv_app_icon).setImageDrawable(currentApp.appIcon)
        view.findViewById<TextView>(R.id.tv_app_name).text = currentApp.appName
        return view
    }
}

步骤3:绑定数据到ListView

// 在Activity的onCreate方法中执行
val listView = findViewById<ListView>(R.id.list_view)
listView.adapter = AppListAdapter(this, installedApps)

// 可选:添加列表项点击事件,打开对应应用
listView.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
    val targetApp = installedApps[position]
    val launchIntent = packageManager.getLaunchIntentForPackage(targetApp.packageName)
    launchIntent?.let { startActivity(it) }
}

关键注意事项

  • Android 11及以上版本必须配置<queries>或QUERY_ALL_PACKAGES权限,否则仅能获取自身应用和少量系统应用。
  • 过滤系统应用的逻辑可按需调整,若需包含系统应用,移除filter中的判断即可。
  • 若使用Java语言,将上述Kotlin代码转换为对应Java语法即可,核心逻辑一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 12:15:09