Android:动态更新app启动图标和应用名

一、需求背景

每逢重要佳节,很多应用启动图标会自动更新为对应佳节的图标,应用无需更新。

二、效果图

更新后的启动图标和应用名称

三、实现流程

Android app只能替换内置的icon,因此需要提前将logo图标放入App资源文件件里

实际项目App更新桌面启动图标由服务器端控制,可以在App启动页里请求全局配置接口,根据接口返回的是否更新启动图标字段值进行处理。
大多数都是用activity-alias方式更新启动图标和应用名称,但是谷歌建议新建activity继承启动页的activity,新建的activity里面是空的。

下面的代码采用新建activity继承启动页的activity方式实现动态更新App启动图标和应用名称

3.1 AndroidManifest.xml代码
bash 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Test"
        tools:targetApi="31">
        <activity
            android:name=".ui.activity.LauncherActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--   更改启动图标和应用名称     -->
        <!--        <activity-alias-->
        <!--            android:name=".ui.activity.NewMainActivity"-->
        <!--            android:enabled="false"-->
        <!--            android:exported="true"-->
        <!--            android:icon="@mipmap/ic_launcher_shlx"-->
        <!--            android:label="@string/app_name2"-->
        <!--            android:targetActivity=".ui.activity.MainActivity">-->
        <!--            <intent-filter>-->
        <!--                <action android:name="android.intent.action.MAIN" />-->

        <!--                <category android:name="android.intent.category.LAUNCHER" />-->
        <!--            </intent-filter>-->

        <!--        </activity-alias>-->

        <activity
            android:name=".ui.activity.LauncherActivityNew"
            android:enabled="false"
            android:exported="true"
            android:icon="@mipmap/ic_launcher_mid_autumn_festival"
            android:label="@string/app_name2"
            android:targetActivity=".ui.activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".ui.activity.MainActivity"
            android:launchMode="standard"
            android:screenOrientation="portrait" />

        <activity
            android:name=".ui.activity.SettingActivity"
            android:launchMode="standard"
            android:screenOrientation="portrait" />
    </application>

</manifest>

注意:

3.2 activity_launcher.xml代码
bash 复制代码
<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.LauncherActivity">

    <TextView
        android:id="@+id/tvSetLogo"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="16dp"
        android:background="@color/black"
        android:gravity="center"
        android:text="启动页"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
3.3 LauncherActivity.kt代码
bash 复制代码
package com.example.test.ui.activity

import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.test.R
import com.example.test.utils.LogUtil


open class LauncherActivity : AppCompatActivity() {
    private lateinit var mPackageManager: PackageManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()
        setContentView(R.layout.activity_launcher)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }

//        LogUtil.i("获取到的路径 simpleName:${ LunchActivity::class.java.simpleName}")
//        LogUtil.i("获取到的路径 packageName:${ LunchActivity::class.java.`package`.name}")
//        LogUtil.i("获取到的路径 name:${ LunchActivity::class.java.name}")
        /*
            获取到的路径 simpleName:LunchActivity
            获取到的路径 packageName:com.example.test.ui.activity
            获取到的路径 name:com.example.test.ui.activity.LunchActivity
         */


        initData()
        initView()
        initEvent()

        if (this::class.java.simpleName.equals(LauncherActivityNew::class.java.simpleName)) {
            LogUtil.i("没有 执行 updateLauncherIcon")
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        } else {
            updateLauncherIcon()
        }
    }

    private fun initData() {
        mPackageManager = applicationContext.packageManager
    }

    private fun initView() {

    }

    private fun initEvent() {

    }

    private fun updateLauncherIcon() {
        LogUtil.i("执行了 updateLauncherIcon")
//            var festervalIcon = ComponentName(baseContext, "com.example.test.ui.activity.LunchActivity")
        var festervalIcon = ComponentName(baseContext, LauncherActivityNew::class.java.name)
        enableComponent(festervalIcon)
        disableComponent(componentName)
        startMainActivity()
    }

    /*
        PackageManager.DONT_KILL_APP
        PackageManager.SYNCHRONOUS
     */

    //启用组件
    private fun enableComponent(componentName: ComponentName) {
        mPackageManager.setComponentEnabledSetting(
            componentName,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
        )
    }

    //隐藏组件
    private fun disableComponent(componentName: ComponentName) {
        mPackageManager.setComponentEnabledSetting(
            componentName,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
        )
    }

    private fun startMainActivity() {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
        this.startActivity(intent)
        finish()
    }
}
3.4 LauncherActivityNew.kt代码
bash 复制代码
class LauncherActivityNew : LauncherActivity() {
}
3.5 activity_main.xml代码
bash 复制代码
<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.LauncherActivity">

    <TextView
        android:id="@+id/tvSetLogo"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="16dp"
        android:background="@color/black"
        android:gravity="center"
        android:text="打开设置页面"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
3.6 MainActivity.kt代码
bash 复制代码
package com.example.test.ui.activity

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.test.R

class MainActivity : AppCompatActivity(R.layout.activity_main) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        findViewById<TextView>(R.id.tvSetLogo).setOnClickListener {
            startActivity(
                Intent(
                    this,
                    SettingActivity::class.java
                )
            )
        }
    }
}
3.7 activity_setting.xml代码
bash 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="设置"
        android:textColor="@color/black"
        android:textSize="20sp" />

</LinearLayout>
3.8 SettingActivity.kt代码
bash 复制代码
package com.example.test.ui.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.test.R

class SettingActivity: AppCompatActivity(R.layout.activity_setting) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
}
相关推荐
coder_pig1 小时前
🤡 公司Android老项目升级踩坑小记
android·flutter·gradle
死就死在补习班3 小时前
Android系统源码分析Input - InputReader读取事件
android
死就死在补习班3 小时前
Android系统源码分析Input - InputChannel通信
android
死就死在补习班3 小时前
Android系统源码分析Input - 设备添加流程
android
死就死在补习班3 小时前
Android系统源码分析Input - 启动流程
android
tom4i3 小时前
Launcher3 to Launchpad 01 布局修改
android
雨白3 小时前
OkHttpClient 核心配置详解
android·okhttp
淡淡的香烟3 小时前
Android auncher3实现简单的负一屏功能
android
RabbitYao4 小时前
Android 项目 通过 AndroidStringsTool 更新多语言词条
android·python
RabbitYao4 小时前
使用 Gemini 及 Python 更新 Android 多语言 Excel 文件
android·python