Android 系统桌面 App —— Launcher 开发

本文围绕Android的Launcher展开,介绍其作为系统桌面管理其他app的功能。详细说明了将app注册为Launcher需在Manifest添加category,还阐述了使用PackageManager扫描手机已安装app信息,以及显示app信息并添加点击启动对应app事件的开发过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Launcher简介

Launcher就是Android系统的桌面,它也是一个app,用于管理其他的app。

注册AndroidManifest

要让app作为Launcher,需要在Manifest中添加两个category:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.HOME"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

此时安装此app之后,点击Home键就会看到以下界面,让你选择使用哪一个桌面应用:


如果选择我们自己开发的 LauncherDemo,就会启动 LauncherDemo 应用。

使用PackageManager扫描所有app

编辑MainActivity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        val apps = scanApps()
        val adapter = AppAdapter(apps)
        rvApps.layoutManager = GridLayoutManager(this, 3)
        rvApps.adapter = adapter
    }

    private fun scanApps():List<ResolveInfo>{
        val intent = Intent(Intent.ACTION_MAIN, null).apply {
            addCategory(Intent.CATEGORY_LAUNCHER)
        }
        return packageManager.queryIntentActivities(intent, 0)
    }
}

我们在MainActivity中使用PackageManager的queryIntentActivities方法扫描出手机上已安装的所有app信息。

activity_main 布局代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity">

	<androidx.recyclerview.widget.RecyclerView
		android:id="@+id/rvApps"
		android:layout_width="match_parent"
		android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

由于布局中使用了 RecyclerView,记得导入 RecyclerView 库:

implementation 'androidx.recyclerview:recyclerview:1.1.0'

显示app信息,添加点击事件

新建AppAdapter类:

class AppAdapter(private val apps: List<ResolveInfo>) : RecyclerView.Adapter<AppAdapter.AppHolder>() {

    private lateinit var context: Context

    class AppHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppHolder {
        context = parent.context
        val view = View.inflate(context, R.layout.item_app, null)
        return AppHolder(view)
    }

    override fun getItemCount(): Int {
        return apps.size
    }

    override fun onBindViewHolder(holder: AppHolder, position: Int) {
        val resolveInfo = apps[position]
        val activityInfo = resolveInfo.activityInfo
        holder.itemView.ivIcon.setImageDrawable(activityInfo.loadIcon(context.packageManager))
        holder.itemView.tvName.text = resolveInfo.loadLabel(context.packageManager)
        holder.itemView.setOnClickListener {
            val intent = Intent().apply {
                component = ComponentName(activityInfo.packageName, activityInfo.name)
            }
            context.startActivity(intent)
        }
    }
}

在此类中使用activityInfo.loadIcon方法加载app图标,使用resolveInfo.loadLabel方法加载app名字,并且添加了点击启动对应app的点击事件。
item_app布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <ImageView
        android:id="@+id/ivIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="36dp"
        android:maxHeight="36dp"
        app:layout_constraintBottom_toTopOf="@id/tvName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:singleLine="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/ivIcon"
        tools:text="@string/app_name" />

</androidx.constraintlayout.widget.ConstraintLayout>

运行程序,效果图如下:

### Android Launcher 的启动流程及时间点 #### 1. **Zygote 进程初始化** Android 系统中的 `Launcher` 是作为应用程序的一部分运行的,其启动过程始于系统的初始进程——`Zygote`。当设备完成引导加载程序 (Bootloader) 和 Linux 内核初始化之后,`init` 进程会创建并启动 `Zygote`[^3]。 `Zygote` 负责预加载通用类库和资源文件,并通过分叉机制快速生成新的应用程序进程。对于 `Launcher` 来说,在系统准备就绪后,它会被标记为默认的 Home 应用程序并通过特定广播触发启动。 --- #### 2. **SystemServer 初始化阶段** 在 `Zygote` 完成初始化后,`SystemServer` 进程被启动。此进程中包含了多个核心服务,其中包括 `ActivityManagerService` 和 `WindowManagerService`。这些服务负责管理应用程序生命周期以及窗口显示逻辑。 特别需要注意的是,`ActivityTaskManagerService` 中的方法 `startHomeOnAllDisplays()` 将会在适当的时间点尝试启动默认的 Home 应用 (`Launcher`): ```java // ActivityTaskManagerService.java public void startHomeOnAllDisplays() { // 遍历所有显示屏并启动对应的 Home 应用 } ``` 上述方法通常是在系统进入用户交互状态时调用,例如锁屏解锁完成后。 --- #### 3. **BroadcastReceiver 接收 ACTION_MAIN 类型 Intent** 一旦系统准备好接收用户输入操作,则会发送一个带有类别 `CATEGORY_HOME` 的隐式意图给当前设置好的默认 Home 应用。此时,`Launcher` 的入口组件(通常是定义在 `AndroidManifest.xml` 文件内的某个活动)将接收到该广播消息[^4]: ```xml <activity android:name=".Launcher"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` 这一步骤标志着实际意义上的 `Launcher` 启动开始。 --- #### 4. **Application 创建与回调执行** 随着目标 Activity 实例化之前,框架层还会先实例化关联的应用对象(`Application`) 并调用其 `onCreate()` 方法。这是通过 `ActivityThread` 组件内部实现的: ```java mInstrumentation.callApplicationOnCreate(app); ``` 这里确保了全局上下文环境已经构建完毕,可以供后续业务模块使用[^2]。 随后便是具体的 UI 渲染工作,比如读取配置数据、绑定视图控件等动作均在此期间发生。 --- #### 5. **Launcher 特定行为分析** 具体到源码层面来看,《LauncherAppState》这个类承担着部分关键职责,像主题样式适配或者快捷方式数据库维护等功能都可能涉及其中[^1]。因此可以说它是整个桌面体验得以正常运作的基础之一。 最终呈现出来的效果就是我们熟悉的主屏幕界面,等待用户的进一步指令。 --- ### 总结 综上所述,从底层硬件加电到最后看到完整的图形化操作系统首页之间经历了一系列复杂而精密的过程。每一个环节都需要紧密配合才能顺利完成任务。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值