Android app本地切换logo和名称

首先呢,在app的AndroidManifest.xml文件里,配置需要替换的logo和app名称
eg:下面配置了一套默认的,两套用于切换的icon和名称
android:enabled: 设为“true”时,就会启用当前别名的Activity,和当前的icon和当前的应用名称: 设为“false”时,表示停止使用当前别名的Activity
android:icon:当前别名的应用图标
android:label:当前别名的应用名称
android:name:别名,命名规则同Actively
android:targetActivity:通过别名调用的实际 Activity 的名称

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cgg.change_icon">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.ChangeIcon">

        <activity android:name=".MainActivity"/>

        <activity-alias
            android:name=".DefaultAliasActivity"
            android:enabled="true"
            android:icon="@mipmap/ic_launcher"
            android:label="ChangeIcon"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias
            android:name=".Alias1Activity"
            android:enabled="false"
            android:icon="@mipmap/app_logo_b"
            android:label="ChangeIcon1"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias
            android:name=".Alias2Activity"
            android:enabled="false"
            android:icon="@mipmap/app_logo_c"
            android:label="ChangeIcon2"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>
    </application>

</manifest>

然后就是代码切换:相关方法介绍一下
通过 PackageManager 在清单文件中所定义的任何组件上切换 启用/停止 状态。
PackageManager.setComponentEnabledSetting
设置包组件(活动、接收器、服务、提供程序)的启用设置。此设置将覆盖组件在其清单中设置的任何启用状态。
参数:
componentName–要启用的组件
newState–组件的新启用状态。
flags–可选的行为标志

package com.cgg.change_icon

import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.RadioGroup
import androidx.annotation.StringDef

class MainActivity : AppCompatActivity() {

    val APP_D = "com.cgg.change_icon.DefaultAliasActivity"
    val APP_1 = "com.cgg.change_icon.Alias1Activity"
    val APP_2 = "com.cgg.change_icon.Alias2Activity"

    private var mPackageManager: PackageManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mPackageManager = applicationContext.packageManager

        findViewById<ImageView>(R.id.iv_0).setOnClickListener {
            ShowComponent(APP_D)
        }

        findViewById<ImageView>(R.id.iv_1).setOnClickListener {
            ShowComponent(APP_1)
        }

        findViewById<ImageView>(R.id.iv_2).setOnClickListener {
            ShowComponent(APP_2)
        }

    }

    private fun ShowComponent(app: String){
        packageManager.run {
            setComponentEnabledSetting(
                componentName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )
            setComponentEnabledSetting(
                ComponentName(baseContext, app),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv_0"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="30dp"/>

    <ImageView
        android:id="@+id/iv_1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/app_logo_b"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/iv_0"
        android:layout_marginTop="30dp"/>

    <ImageView
        android:id="@+id/iv_2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/app_logo_c"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/iv_1"
        android:layout_marginTop="30dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

功能说完了,下面说下缺陷

替换的icon需要打包的时候就预埋到代码里icon切换的时候和闪退一样上过含activity-alias包后,如果再更新不含activity-alias包,会导致 APP 从桌面再也找不到的风险

到此这篇关于Android app本地切换logo和名称的文章就介绍到这了,更多相关Android 切换logo和名称内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Android 给图片加上水印的示例代码(支持logo+文字)

    本文介绍了Android 给图片加上水印的示例代码(支持logo+文字),分享给大家,具体如下: 现在我们想要往图片上打上水印,该水印应符合这样的需求的: 支持logo+文字: 文字信息支持多行展示: 用户可以选择水印在图片上的生成位置(左上.右上.右下和左下). 粗略的结构图低配版大概就长这样... 水印结构图.png 现在提供这样的一种思路去实现这一个需求,我们可以通过自定义一个view,view的布局中包含logo.公司名称和相关信息,这个view就是我们要打上图片的水印. 这样的一个vi

  • Android动态修改应用图标与名称的方法实例

    遇到的坑 这里我把做这个功能中遇到的一些问题写在前面,是为了大家能先了解有什么问题存在,遇到这些问题的时候就不慌了,这里我把应用图标和名称先统一使用icon代替进行说明. 1.动态替换icon,只能替换内置的icon,无法从服务器端获取来更新icon: 2.动态替换icon以后,应用内更新的时候必须要切换到原始icon),否则可能导致更新安装失败(AS上表现为adb运行会失败),或者升级后应用图标出现多个甚至应用图标都不显示的情况(这些问题都可以通过下面我推荐的开发规则解决掉,所以这是一个坑点,

  • Android app本地切换logo和名称

    首先呢,在app的AndroidManifest.xml文件里,配置需要替换的logo和app名称eg:下面配置了一套默认的,两套用于切换的icon和名称android:enabled: 设为“true”时,就会启用当前别名的Activity,和当前的icon和当前的应用名称: 设为“false”时,表示停止使用当前别名的Activityandroid:icon:当前别名的应用图标android:label:当前别名的应用名称android:name:别名,命名规则同Activelyandroi

  • Android App中进行语言的切换

    本篇简单介绍将在Android App中进行语言的切换和使用dragonFace改系统语言. 切换语言 首先需要在res 中创建个若干个不同的value文件夹(例如:values.values-en.value-ja).然后将不同的String.xml文件. 这里为 中.英.日三语切换.(value文件夹命名可以参考下面) 在res目錄下建立不同名稱的values文件來調用不同的語言包 Values文件匯總如下: 中文(中國):values-zh-rCN中文(台灣):values-zh-rTW

  • Android app应用多语言切换功能实现

    本文实例为大家分享了Android app应用实现多语言切换功能,供大家参考,具体内容如下 1.添加多语言文件 在不同的 value 文件夹下(例如 value .value-en.values-zh-rTW 文件夹)添加不同语言的 string.xml 文件,我们的项目添加了英文.简体中文.繁体中文三种语言,如下图所示: Project模式: Android模式: 其中英文需要翻译,繁体如果没有专门翻译的话,可以找个简繁转换网站,直接将简体中文转成繁体中文,推荐一个网站: http://www

  • Android app启动图适配方法实例

    目录 前言 1.设置splash主题 2. splash_bg 设置: 3.splash布局文件设置: 附问题:项目启动时,会有白屏现象(在点击 应用图标到看到启动页之间). 总结 前言 app启动后的白屏问题,默认都是在splash页面加主题配置,主题配置一个背景来达到用户点击app图标就立马启动app的假象,大多情况下,使用背景单一的图片作为启动图,我们在设置背景颜色,就能适配的很不错了(背景颜色+logo图片的模式).但是当启动图不再单一,而且复杂的图形时候,适配就成大问题了,下面介绍我的

  • 一看就懂的Android APP开发入门教程

    工作中有做过手机App项目,前端和android或ios程序员配合完成整个项目的开发,开发过程中与ios程序配合基本没什么问题,而android各种机子和rom的问题很多,这也让我产生了学习android和ios程序开发的兴趣.于是凌晨一点睡不着写了第一个android程序HelloAndroid,po出来分享给其他也想学习android开发的朋友,这么傻瓜的Android开发入门文章,有一点开发基础的应该都能看懂. 一.准备工作 主要以我自己的开发环境为例,下载安装JDK和Android SD

  • App内切换语言详解

    前几天客户提需求,对App增加一个功能,这个功能目前市面上已经很常见,那就是应用内切换语言.啥意思,就是 英.中.法.德.日...语言随意切换. (本案例采用Data-Bingding模式,麻麻再也不用担心我findViewBy不到Id了哈哈,开个玩笑) 先上示例图: 代码实现: 布局文件(Data-Binding模式),很简单就是两行文字 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:a

  • Android横竖屏幕切换小结

    Android手机或平板都会存在横竖屏切换的功能,通常是由物理重力感应触发的,但是有时候也不尽然,通常在设置里面我们可以对手机的横竖屏切换进行关闭. AndroidManifest.xml <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|

  • 使用Chrome浏览器调试Android App详解

    个人一直对Chrome情有独钟,Chrome除了更快之外,对开发者的支持更友好.内置强大的Developer Tools,相信Web开发简直爱不释手!而且Chrome Store里提供各种各样的插件,没有你用不到,只有你想不到.现在任何事基本Chrome全部办的到,有时候就在想,如果可以用Chrome调试Android App该多方便,而如今Facebook刚刚开源了一个工具Stetho,从此Chrome调试Android不再是梦. 调试工具 在Android开发中除了一些官方自带的一些调试工具

  • 实例讲解Android App使用自带的SQLite数据库的基本方法

    SQLite数据库是android系统内嵌的数据库,小巧强大,能够满足大多数SQL语句的处理工作,而SQLite数据库仅仅是个文件而已.虽然SQLite的有点很多,但并不是如同PC端的mysql般强大,而且android系统中不允许通过JDBC操作远程数据库,所以只能通过webservice等手段于php.servlet交互获取数据. 基础 SQLiteDatabase类,代表了一个数据库对象,通过SQLiteDatabase来操作管理数据库. 一些基本的用法: static  SQLiteDa

  • Android 如何修改APK的默认名称

    Android 如何修改APK的默认名称 用Android Studio 打包App时生成的名称默认是 app-release.apk(已签名) 或 app-debug.apk(测试版). 要想打包时修改默认名称,可以打开在build.gradle(module:app)文件,在android{}中添加如下代码: android.applicationVariants.all { variant -> variant.outputs.each { output -> def outputFil

随机推荐