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

张国伟  金牌会员 | 2024-7-27 09:53:58 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 952|帖子 952|积分 2856

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

Launcher简介

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


要让app作为Launcher,需要在Manifest中添加两个category:
  1. <category android:name="android.intent.category.HOME"/>
  2. <category android:name="android.intent.category.DEFAULT"/>
复制代码
添加后的代码
  1. <activity android:name=".MainActivity">
  2.    <intent-filter>
  3.        <action android:name="android.intent.action.MAIN"/>
  4.        <category android:name="android.intent.category.HOME"/>
  5.        <category android:name="android.intent.category.DEFAULT"/>
  6.        <category android:name="android.intent.category.LAUNCHER"/>
  7.    </intent-filter>
  8. </activity>
复制代码
此时安装此app之后,点击Home键就会看到以下界面,让你选择使用哪一个桌面应用:



假如选择我们本身开发的 Launcher App,就会启动 我们本身的桌面应用,目前这个应用是空白的,需要添加应用列表以及相应的点击变乱。

留意:平凡的安卓手机都能看到另外一个界面,但是像小米、华为如许的手机就不行。

使用PackageManager扫描所有app


编辑MainActivity:
  1. public class MainActivity extends AppCompatActivity {
  2.    @Override
  3.    protected void onCreate(Bundle savedInstanceState) {
  4.        super.onCreate(savedInstanceState);
  5.        setContentView(R.layout.activity_main);  
  6.         //获取所有app,设置adapter
  7.        PackageManager pm = getPackageManager();
  8.        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  9.        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  10.        final List<ResolveInfo> activities = pm.queryIntentActivities(mainIntent, 0);
  11.        RecyclerView recyclerView = findViewById(R.id.rv);
  12.        AppAdapter adapter = new AppAdapter(activities, this);
  13.        recyclerView.setAdapter(adapter);
  14.        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
  15.    }
  16. }
复制代码
我们在MainActivity中使用PackageManager的queryIntentActivities方法扫描脱手机上已安装的所有app信息。

activity_main 结构代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     tools:context=".MainActivity">
  7.     <androidx.recyclerview.widget.RecyclerView
  8.         android:id="@+id/rvApps"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="match_parent" />
  11. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码
由于结构中使用了 RecyclerView,记得导入 RecyclerView 库:
  1. implementation 'androidx.recyclerview:recyclerview:1.1.0'
复制代码
显示app信息,添加点击变乱


新建AppAdapter类:
  1. public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ViewHolder> {
  2.    private List<ResolveInfo> mList;
  3.    private Context mContext;
  4.    public AppAdapter(List<ResolveInfo> list, Context context) {
  5.        this.mList = list;
  6.        this.mContext = context;
  7.    }
  8.    @NonNull
  9.    @Override
  10.    public AppAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  11.        View inflate = LayoutInflater.from(mContext).inflate(R.layout.rv_item, parent, false);
  12.        //作为一个view填充
  13.        View view = View.inflate(parent.getContext(), R.layout.rv_item, null);
  14.        return new ViewHolder(view);
  15.    }
  16.    @Override
  17.    public void onBindViewHolder(@NonNull final AppAdapter.ViewHolder holder, final int position) {
  18.        holder.mIcon.setImageDrawable(mList.get(position).loadIcon(mContext.getPackageManager()));
  19.        holder.mTtile.setText(mList.get(position).loadLabel(mContext.getPackageManager()));
  20.        holder.itemView.setOnClickListener(new View.OnClickListener() {
  21.            @Override
  22.            public void onClick(View v) {
  23.                Intent launchIntent = new Intent();
  24.                launchIntent.setComponent(new ComponentName(mList.get(position).activityInfo.packageName,
  25.                        mList.get(position).activityInfo.name));
  26.                mContext.startActivity(launchIntent);
  27.            }
  28.        });
  29.    }
  30.    @Override
  31.    public int getItemCount() {
  32.        return mList == null ? 0 : mList.size();
  33.    }
  34.    public class ViewHolder extends RecyclerView.ViewHolder {
  35.        private ImageView mIcon;
  36.        private TextView mTtile;
  37.        public ViewHolder(@NonNull View itemView) {
  38.            super(itemView);
  39.            mIcon = itemView.findViewById(R.id.iv);
  40.            mTtile = itemView.findViewById(R.id.tv);
  41.        }
  42.    }
  43. }
复制代码
在此类中使用activityInfo.loadIcon方法加载app图标,使用resolveInfo.loadLabel方法加载app名字,并且添加了点击启动对应app的点击变乱。
rv_item结构文件如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    xmlns:app="http://schemas.android.com/apk/res-auto"
  4.    xmlns:tools="http://schemas.android.com/tools"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent"
  7.    android:padding="10dp">
  8.    <ImageView
  9.        android:id="@+id/ivIcon"
  10.        android:layout_width="wrap_content"
  11.        android:layout_height="wrap_content"
  12.        android:maxWidth="36dp"
  13.        android:maxHeight="36dp"
  14.        app:layout_constraintBottom_toTopOf="@id/tvName"
  15.        app:layout_constraintEnd_toEndOf="parent"
  16.        app:layout_constraintStart_toStartOf="parent"
  17.        app:layout_constraintTop_toTopOf="parent"
  18.        tools:src="@mipmap/ic_launcher" />
  19.    <TextView
  20.        android:id="@+id/tvName"
  21.        android:layout_width="wrap_content"
  22.        android:layout_height="wrap_content"
  23.        android:ellipsize="end"
  24.        android:lines="1"
  25.        android:singleLine="true"
  26.        app:layout_constraintBottom_toBottomOf="parent"
  27.        app:layout_constraintEnd_toEndOf="parent"
  28.        app:layout_constraintStart_toStartOf="parent"
  29.        app:layout_constraintTop_toBottomOf="@id/ivIcon"
  30.        tools:text="@string/app_name" />
  31. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码
运行效果





设置桌面配景

首先第一步我们需要先让配景显示出来,在res/valuses/styles.xml文件下添加如下代码:
  1. <style name="LauncherAppTheme" parent="android:Theme.Wallpaper.NoTitleBar">
  2.    <!-- Customize your theme here. -->
  3.    <item name="colorPrimary">@color/colorPrimary</item>
  4.    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  5.    <item name="colorAccent">@color/colorAccent</item>
  6.    <item name="windowNoTitle">true</item>
  7. </style>
复制代码
接着在AndroidManifest.xml中使用这个Theme:
  1. <application
  2.    android:allowBackup="true"
  3.    android:icon="@mipmap/ic_launcher"
  4.    android:label="@string/app_name"
  5.    android:roundIcon="@mipmap/ic_launcher_round"
  6.    android:supportsRtl="true"
  7.    android:theme="@style/LauncherAppTheme">
  8.    ...
复制代码
因为是app关系需要适配状态栏。添加transparentStatusBarForImage方法,在onCreate()的setContentView(R.layout.activity_main);后调用
  1. public void transparentStatusBarForImage(Activity context) {
  2.        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  3.            //5.0 全透明实现
  4.            //getWindow.setStatusBarColor(Color.TRANSPARENT)
  5.            Window window = context.getWindow();
  6.            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  7.            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
  8.            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  9.            window.setStatusBarColor(Color.TRANSPARENT);
  10.        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  11.            //4.4 全透明状态栏
  12.            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  13.        }
  14.    }
复制代码
使用
  1.     @Override
  2.    protected void onCreate(Bundle savedInstanceState) {
  3.        super.onCreate(savedInstanceState);
  4.        setContentView(R.layout.activity_main);
  5.        transparentStatusBarForImage(this);
  6.    }
复制代码
会出现图标也上去的题目,在主界面的xml文件中增加android:fitsSystemWindows="true"即可

app图标巨细不一样的题目,可以通过写死尺寸来控制
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    xmlns:app="http://schemas.android.com/apk/res-auto"
  4.    xmlns:tools="http://schemas.android.com/tools"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent"
  7.    android:padding="10dp">
  8.    <ImageView
  9.        android:id="@+id/iv"
  10.        android:layout_width="48dp"
  11.        android:layout_height="48dp"
  12.        android:scaleType="fitXY"
  13.        app:layout_constraintBottom_toTopOf="@id/tv"
  14.        app:layout_constraintEnd_toEndOf="parent"
  15.        app:layout_constraintStart_toStartOf="parent"
  16.        app:layout_constraintTop_toTopOf="parent"
  17.        tools:src="@mipmap/ic_launcher" />
  18.    <TextView
  19.        android:id="@+id/tv"
  20.        android:layout_width="wrap_content"
  21.        android:layout_height="wrap_content"
  22.        android:ellipsize="end"
  23.        android:lines="1"
  24.        android:singleLine="true"
  25.        app:layout_constraintBottom_toBottomOf="parent"
  26.        app:layout_constraintEnd_toEndOf="parent"
  27.        app:layout_constraintStart_toStartOf="parent"
  28.        app:layout_constraintTop_toBottomOf="@id/iv"
  29.        tools:text="@string/app_name" />
  30. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码


其他题目

1.打开应用后会把华为桌面应用给关掉,怎么做到的?不是关掉,是把回退屏蔽了,不答应退出。home键照旧好用的,回到原主界面
2.锁屏后放置一段时间,它还在?还存活?存活
3.定制度比较低的安卓系统怎么找到对应的系统级别签名?去找Android各个版本的源码,哪里有签名文件

第一个题目
  1.     @Override
  2.    public boolean onKeyDown(int keyCode, KeyEvent event) {
  3.        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
  4. //            Toast.makeText(this, "按下了back键   onKeyDown()", Toast.LENGTH_SHORT).show();
  5.            return false;
  6.        }else {
  7.            return super.onKeyDown(keyCode, event);
  8.        }
  9.    }
复制代码
第二个题目
界面还会在,没有回收。

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

参考

Android 系统桌面 App —— Launcher 开发 recycleview的方式
android手把手教你开发launcher(一)(AndroidStudio版)
Launcher开发——入门篇 另有后续
Android安卓-开发一个android桌面 GridView的方式
Launcher3 包含Launcher3开发的源码解析

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

张国伟

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表