动态切换app图标




android:name="com.fungo.xmsport.icon_tag"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:targetActivity=".splash.SplashActivity">




    
        
            
            
        
    

    
        
            
            
        
    

    
        
            
            
        
    
    
    
    if (DataPreferencesHelper.getPref("isWelcome", false)) {
        //非第一次启动,一键登录预约取号
        when {
            TimeUtils.getToday() == "2020-10-20" -> {
                openComponent( "com.fungo.xmsport.icon_tag")
                disableComponent("com.fungo.xmsport.icon_tag_1")
                disableComponent("com.fungo.xmsport.icon_tag_2")
                disableComponent("com.fungo.xmsport.icon_tag_3")
            }
            TimeUtils.getToday() == "2020-10-21" -> {
                disableComponent( "com.fungo.xmsport.icon_tag")
                openComponent("com.fungo.xmsport.icon_tag_1")
                disableComponent("com.fungo.xmsport.icon_tag_2")
                disableComponent("com.fungo.xmsport.icon_tag_3")
            }
            TimeUtils.getToday() == "2020-10-22" -> {
                disableComponent( "com.fungo.xmsport.icon_tag")
                disableComponent("com.fungo.xmsport.icon_tag_1")
                openComponent("com.fungo.xmsport.icon_tag_2")
                disableComponent("com.fungo.xmsport.icon_tag_3")
            }
            else -> {
                disableComponent( "com.fungo.xmsport.icon_tag")
                disableComponent("com.fungo.xmsport.icon_tag_1")
                disableComponent("com.fungo.xmsport.icon_tag_2")
                openComponent("com.fungo.xmsport.icon_tag_3")
            }
        }
    }
    
    
    
    /**
 * 打开-启用
 */
private fun openComponent(icon_tag: String) {
    LogUtils.d("liuhong : switchIcon icon_tag = $icon_tag")
    try {
            val pm = packageManager
            val actComponentName = ComponentName(
                    baseContext,
                    icon_tag)
            //活动图标新状态
            val actNewState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED
            //新状态跟当前状态不一样才执行
            if (pm.getComponentEnabledSetting(actComponentName) != actNewState) {
                pm.setComponentEnabledSetting(
                        actComponentName,
                        actNewState,
                        PackageManager.DONT_KILL_APP)
            }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

/**
 *  关闭-禁用
 */
private fun disableComponent(icon_tag: String) {
    LogUtils.d("liuhong : switchIcon2 icon_tag = $icon_tag")
    try {
            val pm = packageManager
            val normalComponentName = ComponentName(
                    baseContext,
                    icon_tag)
            //正常图标新状态,此处使用用来修改清单文件中activity-alias下的android:enable的值
            val normalNewState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED
            //新状态跟当前状态不一样才执行
            if (pm.getComponentEnabledSetting(normalComponentName) != normalNewState) {
                //PackageManager.DONT_KILL_APP表示执行此方法时不杀死当前的APP进程
                pm.setComponentEnabledSetting(
                        normalComponentName,
                        normalNewState,
                        PackageManager.DONT_KILL_APP)
            }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

你可能感兴趣的:(动态切换app图标)