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

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

前言

这一遍文章记录了最近一两天开发一个简单的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
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的安卓桌面 launcher 代码示例: ```java public class LauncherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launcher); // 获取应用列表 List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0); // 初始化应用列表视图 ListView listView = (ListView) findViewById(R.id.listView); AppListAdapter adapter = new AppListAdapter(this, apps); listView.setAdapter(adapter); // 处理应用列表项点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); Intent launchIntent = getPackageManager().getLaunchIntentForPackage(app.packageName); startActivity(launchIntent); } }); } private class AppListAdapter extends ArrayAdapter<ApplicationInfo> { private LayoutInflater mInflater; public AppListAdapter(Context context, List<ApplicationInfo> apps) { super(context, R.layout.app_list_item, apps); mInflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = mInflater.inflate(R.layout.app_list_item, null); } ApplicationInfo app = getItem(position); TextView nameTextView = (TextView) view.findViewById(R.id.nameTextView); ImageView iconImageView = (ImageView) view.findViewById(R.id.iconImageView); nameTextView.setText(app.loadLabel(getPackageManager())); iconImageView.setImageDrawable(app.loadIcon(getPackageManager())); return view; } } } ``` 这里使用了一个 ListView 来显示已安装的应用列表,点击列表项可以启动该应用。需要注意的是,需要在 AndroidManifest.xml 文件中声明该 Activity,并设置为默认启动项: ```xml <activity android:name=".LauncherActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值