【安卓小笔记】自己制作一个Launcher

这篇博客详细记录了作者创建一个自定义Android Launcher的过程,包括在AndroidManifest.xml中的配置、获取已安装应用信息、显示应用列表、实现壁纸更换以及遇到的遗留问题,如ViewPager切换动画和图标拖拽到第二页的挑战。

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

前言

这一遍文章记录了最近一两天开发一个简单的Launcher,好记性不如烂笔头,在这里记录一遍可以加深下印象,顺便记录下遗留的问题以便后续修改。

1.Launcher的概述

android系统启动的最后一步就是启动一个HOME应用程序,来展示系统已经安装的应用程序,这个就是Launcher。系统已经安装的应用程序信息可以从PMS获取到,然后展示到Launcher上,这样就可以通过点击启动相应的应用程序。

2.定义AndroidManifest.xml

首先我们新建一个工程,然后编辑AndroidManifest.xml,可以参照android系统的Launcher2/Launcher3的来写:

<activity android:name=".MainActivity"
            android:launchMode="singleTask"
            android:clearTaskOnLaunch="true"
            android:stateNotNeeded="true"
            android:resumeWhilePausing="true"
            android:theme="@style/Launcher"
            android:windowSoftInputMode="adjustPan"
            android:screenOrientation="nosensor">
            <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.MONKEY"/>

            </intent-filter>
        </activity>

其实Launcher跟我们开发其他应用差不多,主要多了一下两个category:

<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>

还有要注意下设置启动模式singleTask。

这样把应用程序安装到手机上当我们按下Home键就可以选择使用这个Launcher了。

3.获取已安装应用程序信息

方案1
这样可以获取到已安装的包信息,参数0表示不接受任何flag信息。但是这个接口给有些手机厂商把权限禁止后,就不能正确获取到已安装应用列表了,所以不推荐用这个接口。

 PackageManager pm = getPackageManager();
 List<PackageInfo> list = pm.getInstalledPackages(0);

方案2
PackageManager().queryIntentActivities(Intent intent,int flags)可以传个Intent参数,传进去的Intent添加一个Intent.CATEGORY_LAUNCHER的Category就可以查询到需要的信息。

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);

4.展示应用程序

展示应用程序有很多种方法,这个也就是自定义一个Launcher真正困难所在。我现在水平有限只能想到什么就做成什么样,刚开始我就直接用GridView直接展示出来,但是这样感觉跟原生的差太多了,然后我用ViewPager+RecyclerView替代了。用ViewPager是可以实现翻页,多页的切换。然后用RecyclerView只是为了可以实现退拽功能。但是现在还不能实现从当前页退拽到下一页。

这里还依赖了一个指示器,因为这个之前用过感觉还可以就直接拿来用了。

implementation 'me.relex:circleindicator:1.2.2@aar'

activity_main.xml用Viewpager+CircleIndicator,这里的current_circle就是指示器用的小圆点我是用shape画了一个圆。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fi
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值