Android 系统桌面 App —— Launcher 开发(1)

news2024/9/29 9:29:10

Android 系统桌面 App —— Launcher 开发(1)

Launcher简介

Launcher就是Android系统的桌面,俗称“HomeScreen”也就是我们开机后看到的第一个App。launcher其实就是一个app,它的作用是显示和管理手机上其他App。目前市场上有很多第三方的launcher应用,比如“小米桌面”、“91桌面”等等

注册AndroidManifest

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

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

添加后的代码

<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键就会看到以下界面,让你选择使用哪一个桌面应用:

如果选择我们自己开发的 Launcher App,就会启动 我们自己的桌面应用,目前这个应用是空白的,需要添加应用列表以及相应的点击事件。

注意:普通的安卓手机都能看到另外一个界面,但是像小米、华为这样的手机就不行。

使用PackageManager扫描所有app

编辑MainActivity:

public class MainActivity extends AppCompatActivity {
​
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);   
        //获取所有app,设置adapter
        PackageManager pm = getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        final List<ResolveInfo> activities = pm.queryIntentActivities(mainIntent, 0);
        RecyclerView recyclerView = findViewById(R.id.rv);
        AppAdapter adapter = new AppAdapter(activities, this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
    }
}

我们在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类:

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ViewHolder> {
​
    private List<ResolveInfo> mList;
    private Context mContext;
​
    public AppAdapter(List<ResolveInfo> list, Context context) {
        this.mList = list;
        this.mContext = context;
    }
​
    @NonNull
    @Override
    public AppAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.rv_item, parent, false);
        //作为一个view填充
        View view = View.inflate(parent.getContext(), R.layout.rv_item, null);
        return new ViewHolder(view);
    }
​
    @Override
    public void onBindViewHolder(@NonNull final AppAdapter.ViewHolder holder, final int position) {
        holder.mIcon.setImageDrawable(mList.get(position).loadIcon(mContext.getPackageManager()));
        holder.mTtile.setText(mList.get(position).loadLabel(mContext.getPackageManager()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent launchIntent = new Intent();
                launchIntent.setComponent(new ComponentName(mList.get(position).activityInfo.packageName,
                        mList.get(position).activityInfo.name));
                mContext.startActivity(launchIntent);
            }
        });
    }
​
    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }
​
    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIcon;
        private TextView mTtile;
​
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mIcon = itemView.findViewById(R.id.iv);
            mTtile = itemView.findViewById(R.id.tv);
        }
    }
}

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

rv_item布局文件如下:

<?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>

运行效果

设置桌面背景

首先第一步我们需要先让背景显示出来,在res/valuses/styles.xml文件下添加如下代码:

<style name="LauncherAppTheme" parent="android:Theme.Wallpaper.NoTitleBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
</style>

接着在AndroidManifest.xml中使用这个Theme:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/LauncherAppTheme">
    ...

因为是app关系需要适配状态栏。添加transparentStatusBarForImage方法,在onCreate()的setContentView(R.layout.activity_main);后调用

public void transparentStatusBarForImage(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            //getWindow.setStatusBarColor(Color.TRANSPARENT)
            Window window = context.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

使用

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        transparentStatusBarForImage(this);
    }

会出现图标也上去的问题,在主界面的xml文件中增加android:fitsSystemWindows="true"即可

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/iv"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toTopOf="@id/tv"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@mipmap/ic_launcher" />
​
    <TextView
        android:id="@+id/tv"
        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/iv"
        tools:text="@string/app_name" />
​
</androidx.constraintlayout.widget.ConstraintLayout>

其他问题

1.打开应用后会把华为桌面应用给关掉,怎么做到的?不是关掉,是把回退屏蔽了,不允许退出。home键还是好用的,回到原主界面

2.锁屏后放置一段时间,它还在?还存活?存活

3.定制度比较低的安卓系统怎么找到对应的系统级别签名?去找Android各个版本的源码,哪里有签名文件

第一个问题

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//            Toast.makeText(this, "按下了back键   onKeyDown()", Toast.LENGTH_SHORT).show();
            return false;
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }

第二个问题

界面还会在,没有回收。

注:这篇文章只是简单的桌面app实现

参考

Android 系统桌面 App —— Launcher 开发 recycleview的方式

android手把手教你开发launcher(一)(AndroidStudio版)

Launcher开发——入门篇 还有后续

Android安卓-开发一个android桌面 GridView的方式

Launcher3 包含Launcher3开发的源码解析

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/920632.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

激活函数总结(十八):激活函数补充(RBF、SQ-RBF)

激活函数总结&#xff08;十八&#xff09;&#xff1a;激活函数补充 1 引言2 激活函数2.1 Radial Basis Function (RBF)激活函数2.2 Square Radial Basis Function&#xff08;SQ-RBF&#xff09;激活函数 3. 总结 1 引言 在前面的文章中已经介绍了介绍了一系列激活函数 (Sig…

《动手学深度学习》-28批量归一化

沐神版《动手学深度学习》学习笔记&#xff0c;记录学习过程&#xff0c;详细的内容请大家购买书籍查阅。 b站视频链接 开源教程链接 批量归一化 将参数的量级进行统一&#xff0c;使得深度神经网络更好收敛&#xff1a; γ \gamma γ和 β \beta β是可学习的参数&#xf…

Shader Graph24-摇晃树叶

一、开始Unreal&#xff0c;新建Materail命名为DemoUVRotation。 二、在网上找个素材&#xff0c;去掉背景&#xff0c;保证树根是在边上的位置 三、换上我们的图片&#xff0c;在time节点后添加一个sine&#xff0c;让他的位置是周期性的就行了。 四、结果

Java接收前端请求体方式

&#x1f497;wei_shuo的个人主页 &#x1f4ab;wei_shuo的学习社区 &#x1f310;Hello World &#xff01; 文章目录 RequestBodyPathVariableRequestParamValidated方法参数校验方法返回值校验 RequestHeaderHttpServletRequest ## Java接收前端请求体的方式 请求体&#xf…

Python - 面向对象的属性,类方法,静态方法,实例方法的区别及用法详解

一. 前言 在Python的面向对象编程中&#xff0c;类属性和实例属性是两个不同的概念&#xff0c;它们在作用域和使用方式上有所区别。在Python中的面向对象编程中有三种方法&#xff1a;实例方法、类方法和静态方法&#xff0c;它们之间的差异主要体现在参数传递和调用方式上。…

java八股文面试[数据结构]——集合框架

Java集合体系框架 Java集合类主要由两个根接口Collection和Map派生出来的。 Collection派生出了三个子接口&#xff1a; Map接口派生&#xff1a; Map代表的是存储key-value对的集合&#xff0c;可根据元素的key来访问value。 因此Java集合大致也可分成List、Set、Queue、Map…

赋能道路交通场景,九州未来助力建成广西交科车路协同智能网联示范园区

智慧高速车路协同需要更加稳固的技术基座 智慧高速是中国高速公路建设的热点之一&#xff0c;车路协同又是未来智慧高速建设的核心内容。高速公路运行环境相对简单、主体权责清晰、路侧机电设施齐全&#xff0c;具备开展车路协同创新示范的良好条件。 广西交科集团&#xff0…

使用element-plus组件,默认显示英文 转换为中文

最近在边写项目边学习vue3 所以这几天没有更新 找机会把vue3的知识也统计一下吧 先说今天遇到的问题 最近做项目的时候使用element-plus分页组件时发现&#xff0c;显示的不是中文的了&#xff0c;是英文的 解决方法 在app.vue里面配置 <template><el-config-provi…

【ES】笔记-生成器函数与调用

function* function* 这种声明方式 (function关键字后跟一个星号&#xff09;会定义一个生成器函数 (generator function)&#xff0c;它返回一个 Generator 对象。 function * gen(){console.log("heloo generator")}如何调用呢&#xff1f;先看下里面的内容 返回…

Dockerfile快速搭建自己专属的LAMP环境,生成镜像lamp:v1.1,并推送到私有仓库

环境&#xff1a; CentOS 7 Linux 3.10.0-1160.el7.x86_64 具体要求如下&#xff1a; &#xff08;1&#xff09;基于centos:6基础镜像&#xff1b; &#xff08;2&#xff09;指定作者信息&#xff1b; &#xff08;3&#xff09;安装httpd、mysql、mysql-server、php、ph…

【锐捷】OSPF 多区域配置

【实验名称】 配置 OSPF 多区域。 【实验目的】 配置 OSPF 多区域&#xff0c;理解 OSPF 层次型网络的特点。 【背景描述】 本实验拓扑图中有 3 台路由器&#xff0c;路由器在区域 0 和区域 1 中&#xff0c;路由器 B 在区域 0 和区域 30&#xff0c; 路由器 C 在区域 30。 【需…

简单着色器编写(下)

函数部分介绍完了&#xff0c;最后来介绍一下main函数中的部分。 std::string vertexShader "#version 330 core\n" "\n" "layout(location0)in vec4 position;" "\n" "void main()\n" "{\n&…

day43参与通信的服务器

1.题目描述 这里有一幅服务器分布图&#xff0c;服务器的位置标识在 m * n 的整数矩阵网格 grid 中&#xff0c;1 表示单元格上有服务器&#xff0c;0 表示没有。 如果两台服务器位于同一行或者同一列&#xff0c;我们就认为它们之间可以进行通信。 请你统计并返回能够与至少…

数据分析实战│时间序列预测

时间序列预测问题是一类常见的数据分析问题。数据中往往包含时间标签,这类问题往往根据过去一段时间的数据,建立能够比较精确地反映序列中所包含的动态依存关系的数学模型,并对未来的数据进行预测。 01、问题描述及数据挖掘目标 本案例给出二战时期的某气象站温度记录值,通…

Terraform(二)

Terraform实践 1. Terraform Docker Example1.1 Install Terraform1.2 Verify the installation1.3 Enable tab completion1.4 Quick start tutorial To deploy infrastructure with Terraform: Scope - Identify the infrastructure for your project.Author - Write the con…

【校招VIP】前端校招考点之行内/块级元素

考点介绍&#xff1a; 行内元素会在一条直线上排列&#xff08;默认宽度只与内容有关&#xff09;&#xff0c;都是同一行的&#xff0c;水平方向排列。块级元素各占据一行&#xff08;默认宽度是它本身父容器的100%&#xff08;和父元素的宽度一致&#xff09;&#xff0c;与内…

抖音书单背景图制作怎么做?分享两个制作小妙招

相信屏幕前的小伙伴今天有刷抖音吧&#xff01;由于迎合受众对碎片化传播的需求&#xff0c;抖音也成为了当下最流行的短视频应用之一&#xff0c;在抖音上分享书单已经成为了一种流行的趋势&#xff0c;而制作一张吸引人的书单背景图是很重要的。在本文中&#xff0c;我们将分…

Python Web开发技巧X

目录 select_related 和 prefetch_related 生成器对象的三种创建方式 classmethod和staticmethod __class__属性 python创建一个类会依次去调用哪些方法 __new__和__init__实现单例模式的饿汉式和懒汉式 select_related 和 prefetch_related select_related 和 prefetch_…

HTML中SCRIPT 标签中的那些属性

在HTML中&#xff0c; <script> 标签用于嵌入或引用JavaScript代码。 在 <script> 标签中&#xff0c;有两个属性可以用来控制脚本的加载和执行方式&#xff1a; async 和 defer 。 当然这也是常见的一道面试题&#xff0c; async 和 defer 的作用和区别。 asy…

【IO流中的字节流(InputStream)(OutputStream)】

字符集 美国人发明计算机 要将他们的字符存入计算机&#xff08;英文字母、数字、标点、特殊字符&#xff09; 给字符进行编号&#xff0c;组成了一张ASCII码表&#xff08;美国信息交换标准代码&#xff09;&#xff0c;一共包含128个字符 该码表以1个字节存储1个字符&#xf…