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

这篇具有很好参考价值的文章主要介绍了Android 系统桌面 App —— Launcher 开发(1)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

如果选择我们自己开发的 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>

运行效果

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

设置桌面背景

首先第一步我们需要先让背景显示出来,在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开发的源码解析文章来源地址https://www.toymoban.com/news/detail-677502.html

到了这里,关于Android 系统桌面 App —— Launcher 开发(1)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android Studio App开发之通知渠道NotificationChannel及给华为、小米手机桌面应用添加消息数量角标实战(包括消息重要级别的设置 附源码)

    需要全部源码或运行有问题请点赞关注收藏后评论区留言~~~ 为了分清消息通知的轻重缓急,Android8.0新增了通知渠道,并且必须指定通知渠道才能正常推送消息,一个应用允许拥有多个通知渠道,每个渠道的重要性各不相同,有的渠道消息在通知栏被折叠成小行,有的渠道消

    2024年02月16日
    浏览(43)
  • 搭载基于RK3229的Android5.1修改开机默认桌面Launcher

    在..rk3229_5.1_boxframeworksbaseservicescorejavacomandroidserveram目录找到ActivityManagerService.java文件。在文件里找到startHomeActivityLocked函数里的setDefaultLauncher。 在setDefaultLauncher设置开机默认桌面launch的包名。我开发的固件,所要启动的包名如下:          String  packageName = SystemP

    2024年02月06日
    浏览(37)
  • android studio开发app实例

    以下是一个简单的Android Studio开发App的实例: 1. 打开Android Studio,并创建一个新项目。 2. 选择一个适当的应用程序名称和包名称,然后选择目标API级别和默认Activity的模板。 3. 在MainActivity.java文件中,添加以下代码以配置MainActivity: ``` import android.os.Bundle; import androidx.appcompa

    2024年02月16日
    浏览(40)
  • 基于Android的学生信息管理App设计(Android studio开发)

    目 录 一、 题目选择(题目、选题意义) 3 二、 设计目的 3 1、 初衷 3 2、 结合实际 3 3、 使用工具 3 三、 最终页面效果展示 4 1、 登陆界面 4 2、 主界面 5 3、 各个功能模块 6 四、 各部分设计 11 1、活动页面Activity布局文件 11 2、Activity的编程 13 五、 总结 17 题目:基于Android的

    2024年02月08日
    浏览(86)
  • 基于Android Studio的记账类app开发

    记账 APP 需要有如下三个系统: 统计系统、记账系统、用户系统 。 统计系统需要实现当月消费统计,包括收入、支出、结余等内容, 并可以让用户通过可视化图的方式清晰了解使用情况。 记账系统需要实现记账的操作,包括选择账 目类别、消费类型、金额、具体内容等,

    2023年04月08日
    浏览(38)
  • Android Studio开发简易APP添加代办事项

    创建xml布局页

    2024年02月15日
    浏览(32)
  • 移动开发项目 Android Studio 健康助手APP

    健康助手系统是一款便捷软件,旨在通过提供多方面的的健康便捷的管理服务,让用户的生活更健康,更便捷。用户可以在健康助手APP上购买不同的体检套餐,预约医生,使用地图查找药房等的位置,浏览网页了解健康知识,传播健康文化。 (1)为了更好地了解自己的身体

    2024年02月03日
    浏览(57)
  • Android 13.0 Launcher3定制化之桌面分页横线改成圆点显示功能实现

    在13.0的系统开发中,在进行launcher3的定制化中,在双层改为单层的开发中,在原生的分页 是横线,而为了美观就采用了系统原来的另外一种分页方式,就是圆点比较美观,接下来就来分析下相关的实现,然后实现其功能 在Launcher3中的核心布局中,最核心的就是workspace hotse

    2024年02月11日
    浏览(54)
  • Android studio课程设计开发实现---日记APP

    你们好,我是oy,介绍一个简易日记APP。 1.启动页、引导页及登陆注册 2.日记相关功能 3.个人中心界面 实现应用启动页及引导页 实现设置密码进入APP,对密码进行加密处理 实现底部导航栏,分为日记列表,新建日记,个人中心模块 实现对日记删除、修改、新增的基础功能

    2024年02月03日
    浏览(51)
  • Android studio 使用C/C++开发app

    一、在新建项目中选择Native C++,然后下一步 二、名字跟保存位置自定义,语言选择Java,SDK根据app安装的平台选择对应的版本,选好后下一步  三、C++ Standard默认选择就可以,点击完成  四、加载完成后,把项目标题Android换成项目  换完后的列表    C/C++的代码在cpp文件夹中

    2024年02月12日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包