Android 系统桌面 App —— Launcher 开发

Android 系统桌面 App —— Launcher 开发

文章目录

Launcher简介注册AndroidManifest使用PackageManager扫描所有app显示app信息,添加点击事件

Launcher简介

Launcher就是Android系统的桌面,它也是一个app,用于管理其他的app。

注册AndroidManifest

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

此时安装此app之后,点击Home键就会看到以下界面,让你选择使用哪一个桌面应用:

如果选择我们自己开发的 LauncherDemo,就会启动 LauncherDemo 应用。

使用PackageManager扫描所有app

编辑MainActivity:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

initView()

}

private fun initView() {

val apps = scanApps()

val adapter = AppAdapter(apps)

rvApps.layoutManager = GridLayoutManager(this, 3)

rvApps.adapter = adapter

}

private fun scanApps():List{

val intent = Intent(Intent.ACTION_MAIN, null).apply {

addCategory(Intent.CATEGORY_LAUNCHER)

}

return packageManager.queryIntentActivities(intent, 0)

}

}

我们在MainActivity中使用PackageManager的queryIntentActivities方法扫描出手机上已安装的所有app信息。

activity_main 布局代码:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:id="@+id/rvApps"

android:layout_width="match_parent"

android:layout_height="match_parent" />

由于布局中使用了 RecyclerView,记得导入 RecyclerView 库:

implementation 'androidx.recyclerview:recyclerview:1.1.0'

显示app信息,添加点击事件

新建AppAdapter类:

class AppAdapter(private val apps: List) : RecyclerView.Adapter() {

private lateinit var context: Context

class AppHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppHolder {

context = parent.context

val view = View.inflate(context, R.layout.item_app, null)

return AppHolder(view)

}

override fun getItemCount(): Int {

return apps.size

}

override fun onBindViewHolder(holder: AppHolder, position: Int) {

val resolveInfo = apps[position]

val activityInfo = resolveInfo.activityInfo

holder.itemView.ivIcon.setImageDrawable(activityInfo.loadIcon(context.packageManager))

holder.itemView.tvName.text = resolveInfo.loadLabel(context.packageManager)

holder.itemView.setOnClickListener {

val intent = Intent().apply {

component = ComponentName(activityInfo.packageName, activityInfo.name)

}

context.startActivity(intent)

}

}

}

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

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

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" />

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" />

运行程序,效果图如下:

相关推荐

mc怎么做轰炸机 mc超简单的红石轰炸机
365bet篮球比分直播

mc怎么做轰炸机 mc超简单的红石轰炸机

📅 09-19 👁️ 1515
五年拍了十部的《鬼吹灯》系列,终于有望迎来最佳版本
win7设置不锁屏不休眠
365体育中国

win7设置不锁屏不休眠

📅 08-27 👁️ 2171
阴阳师涂壁哪里多 阴阳师涂壁在哪里最多
365bet篮球比分直播

阴阳师涂壁哪里多 阴阳师涂壁在哪里最多

📅 09-19 👁️ 5399
Windows安全基础——Windows WMI详解
365体育中国

Windows安全基础——Windows WMI详解

📅 08-16 👁️ 1943
第15章 环节动物门
365体育中国

第15章 环节动物门

📅 08-24 👁️ 1560