android 原生launcher界面,Android framework 使用自定的activity取代默认的Launcher界面

本文介绍了如何在Android系统中用自定义的Activity替换默认的Launcher界面,详细讲解了启动流程、Intent设置以及解决PMS优先级调整的问题,通过修改Activity配置达到开机启动和Home键跳转到自定义界面的效果。

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

背景:需要实现使用自定的activity取代默认的Launcher界面

a. 开机启动后进入自定义的activity

b. 按home键跳转到自定义的activity

1.先看一下launcher的流程

5f90b2a5efbb

image

boolean startHomeActivityLocked(int userId, String reason) {

if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL

&& mTopAction == null) {

// We are running in factory test mode, but unable to find

// the factory test app, so just sit around displaying the

// error message and don't try to start anything.

return false;

}

Intent intent = getHomeIntent();

ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);

if (aInfo != null) {

intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));

// Don't do this if the home app is currently be

### 更换或自定Android汽车系统中的默认LauncherAndroid车机系统中,更换或自定默认Launcher可以通过多种方式实现。以下是具体方法及其背后的原理: #### 方法一:通过用户交互手动设置 如果目标是让用户能够自行选择默认Launcher,则无需修改任何源码即可完成此操作。当设备上存在多个具有`CATEGORY_LAUNCHER`属性的应用时,系统会在首次启动时提示用户选择默认Launcher,并提供选项以决定是否始终使用Launcher。 - **关键点** 应用程序需要在其清单文件(`AndroidManifest.xml`)中声明自己是一个Launcher应用[^1]。这通常涉及配置如下内容: ```xml <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" /> </intent-filter> </activity> ``` #### 方法二:通过AIDL接口强制设定默认Launcher 对于某些定制化的场景(如OEM厂商的需求),可能希望绕过用户的干预直接指定某个特定的Launcher作为默认值。此时可借助Android框架层提供的服务来达成目的。 - **核心逻辑** 需要在Framework层面新增一个用于管理默认Launcher的服务类——LgyManagerService.java,并暴露对应的Binder接口给外部调用者访问[^3]。 - 定义AIDL文件以便客户端能跨进程调用Server端的方法: ```aidl package android.os; /** * @hide */ interface ILgyManager { void setDefaultLauncher(String packageName); } ``` - 实现具体的业务逻辑于LgyManagerService内部,例如验证传入参数合法性、更新数据库记录等动作。 - **注意事项** 此种方案适用于拥有完整ROM权限的情况;而对于普通开发者而言并不适用,因为涉及到对原生系统的改动且需重新编译整个项目才能生效。 #### 方法三:基于现有机制优化多Launcher共存体验 针对运行环境较为宽松的情形下(比如允许安装来自未知来源APK文件),还可以考虑利用已有API简化流程。即通过对Intent解析过程加以控制使得最终仅保留唯一候选项达到相同效果[^4]。 - **技术细节** 主要是调整`ResolverActivity.rebuildList()`函数的行为模式,在构建可供挑选列表之前预先筛选符合条件的目标集合,剔除不符合预期标准条目之后再呈现给前端界面渲染引擎处理。 --- ```python def filter_default_launcher(resolve_list, default_package_name): """ Filters the list of resolvable activities to only include the specified default launcher. Args: resolve_list (list): List of all possible launchers. default_package_name (str): Name of the desired default launcher's package. Returns: list: A filtered list containing only the default launcher if it exists within `resolve_list`. """ return [item for item in resolve_list if item.activityInfo.packageName == default_package_name] # Example usage demonstrating how this function might be applied during rebuildList() filtered_launchers = filter_default_launcher(current_resolve_list, "com.example.customlauncher") if len(filtered_launchers) > 0: current_resolver_activity.set_as_default(filtered_launchers[0]) else: raise Exception("Specified default launcher not found!") ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值