Android学习21 -- launcher

devtools/2025/2/5 5:39:50/

1 前言

之前在工作中,第一次听到launcher有点蒙圈,不知道是啥,当时还赶鸭子上架去和客户PK launcher的事。后来才知道其实就是安卓的桌面。本来还以为很复杂,毕竟之前接触过windows的桌面,那叫一个复杂。。。

后面查了一下,Android的桌面倒是没那么复杂,本质其实就是一个App,系统启动之后跑起来的第一个app。

Android Launcher 是一个应用程序,用于显示设备的主屏幕,并且是启动其他应用的入口。简单来说,Launcher 是 Android 设备的“应用启动器”,负责管理和展示设备的主屏幕、应用图标、桌面小部件(widgets)以及应用程序的启动行为。

2 操作步骤

最近搭建了app的环境,现在借着之前搭建的环境(Android学习20 -- 手搓App2(Gradle)-CSDN博客),试着弄一个launcher。

*   The key to making your app a launcher is to declare the correct intent filter in your `AndroidManifest.xml` file. This tells the Android system that your app is capable of acting as a launcher.
*   Here's how your `AndroidManifest.xml` should look (specifically the `<activity>` tag for your

所以修改之后的AndroidManifest.xml是这样。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.simpleapp"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="SimpleApp"android:theme="@style/Theme.AppCompat.DayNight"><activityandroid:name=".MainActivity"android:label="SimpleApp"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /><category android:name="android.intent.category.HOME" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity></application>
</manifest>

最核心的就是在intent filter里面的配置,重点就是这几行。

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

 * **Explanation:** *

`<action android:name="android.intent.action.MAIN" />`: This is the standard action for the main entry point of an app. * `

<category android:name="android.intent.category.LAUNCHER" />`: This category indicates that this activity should be listed in the app launcher (the list of apps). * `

<category android:name="android.intent.category.HOME" />`: This is the crucial category that tells the system this activity can act as a home screen replacement. * `

<category android:name="android.intent.category.DEFAULT" />`: This category is often used with `HOME` to ensure your app is considered a valid home screen option. * 

MainActivity.java。主界面上面就是一个按钮,点了之后会启动一个浏览器。

package com.example.simpleapp;import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取按钮控件Button openBrowserButton = findViewById(R.id.openBrowserButton);// 设置按钮点击事件openBrowserButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 启动浏览器Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));startActivity(browserIntent);}});}
}

这里就实现了前面说的启动应用程序的功能,就是通过startActivity(browserIntent);

之后要指定布局,也就是res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"><!-- 启动浏览器按钮 --><Buttonandroid:id="@+id/openBrowserButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Open Browser" />
</LinearLayout>

编译用之前调好的就行。

gradle wrapper
gradlew assembleDebug

编译的时候会报错,说mipmap,drawable找不到。

可以去Android Studio创建好的工程里面拷贝过来。完了大概是这样的。里面也就是一堆xml。

之后就可以顺利编过,安装也比较简单。直接运行就是这样:

3 未能最终完成。。

但是有一点很遗憾,在荣耀的平板里面,这个是叫做桌面。自己做的这个launcher,怎么都没法设置进去。除了默认的华为桌面,找不到可以设置的地方。

查了一下,有可能是我的设置还有点问题,但是大概率还是华为的平板把这部分的权限给关了。估计只有以后找一个干净的AOSP再试试了。。。 


http://www.ppmy.cn/devtools/156188.html

相关文章

unity学习26:用Input接口去监测: 鼠标,键盘,虚拟轴,虚拟按键

目录 1 用Input接口去监测&#xff1a;鼠标&#xff0c;键盘&#xff0c;虚拟轴&#xff0c;虚拟按键 2 鼠标 MouseButton 事件 2.1 鼠标的基本操作 2.2 测试代码 2.3 测试情况 3 键盘Key事件 3.1 键盘的枚举方式 3.2 测试代码同上 3.3 测试代码同上 3.4 测试结果 4…

Fortunately 和 luckily区别

Fortunately 和 luckily 的确是同义词&#xff0c;都表示“幸运地”&#xff0c;用于描述某件事发生得很幸运&#xff0c;带有积极、正面的含义。然而&#xff0c;尽管它们的意思相近&#xff0c;fortunately 和 luckily 在使用上有一些细微的差别。 1. 含义相似 Fortunately…

C++ 实现Arp断网

此程序由AI生成&#xff0c;测试过了&#xff0c;可以使用 但是&#xff0c;貌似全部都会断网 #include <pcap.h> #include <WinSock2.h> #include <iostream> #include <vector> #include <string> #include <sstream> #include <iom…

DeepSeek超越ChatGPT的能力及部分核心原理

DeepSeek超越ChatGPT的能力及部分核心原理 目录 DeepSeek超越ChatGPT的能力及部分核心原理超越ChatGPT的能力核心原理超越ChatGPT的能力 推理计算能力更强:在复杂的数学计算、法律文件审查等任务中,DeepSeek的推理能力可媲美甚至超越部分国际顶尖AI模型,包括ChatGPT。例如在…

Python-基于PyQt5,pdf2docx,pathlib的PDF转Word工具(专业版)

前言:日常生活中,我们常常会跟WPS Office打交道。作表格,写报告,写PPT......可以说,我们的生活已经离不开WPS Office了。与此同时,我们在这个过程中也会遇到各种各样的技术阻碍,例如部分软件的PDF转Word需要收取额外费用等。那么,可不可以自己开发一个小工具来实现PDF转…

图的基本术语——非八股文

我之前只看到了数据结构与算法的冰山一角&#xff0c;感觉这些术语只会让知识越来越难理解&#xff0c;现在来看&#xff0c;他们完美抽象一些概念和知识&#xff0c;非常重要。 本篇概念肯定总结不全&#xff0c;只有遇到的会写上&#xff0c;持续更新&#xff0c;之前文章已经…

Linux03——常见的操作命令

root用户以及权限 Linux系统的超级管理员用户是&#xff1a;root用户 su命令 可以切换用户&#xff0c;语法&#xff1a;su [-] [用户名]- 表示切换后加载环境变量&#xff0c;建议带上用户可以省略&#xff0c;省略默认切换到root su命令是用于账户切换的系统命令&#xff…

智云-一个抓取web流量的轻量级蜜罐-k8s快速搭建教程

智云-一个抓取web流量的轻量级蜜罐-k8s快速搭建教程 github地址 https://github.com/xiaoxiaoranxxx/POT-ZHIYUN k8s搭建教程 首先下载代码文件 git clone https://github.com/xiaoxiaoranxxx/POT-ZHIYUN.git cd POT-ZHIYUN编译镜像 代码相关文件在github https://github.com/x…