Android 系統桌面 App —— Launcher 開發

Launcher簡介

Launcher就是Android系統的桌面,它也是一個app,用於管理其他的app。

註冊AndroidManifest

要讓app作爲Launcher,需要在Manifest中添加兩個category:

<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鍵就會看到以下界面,讓你選擇使用哪一個桌面應用:


如果選擇我們自己開發的 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<ResolveInfo>{
        val intent = Intent(Intent.ACTION_MAIN, null).apply {
            addCategory(Intent.CATEGORY_LAUNCHER)
        }
        return packageManager.queryIntentActivities(intent, 0)
    }
}

我們在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類:

class AppAdapter(private val apps: List<ResolveInfo>) : RecyclerView.Adapter<AppAdapter.AppHolder>() {

    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佈局文件如下:

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

運行程序,效果圖如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章