Android 动态替换手机桌面图标

现在淘宝,京东等App在节假日打开时,都是采用一套节假日的图标,这种不用发版本,又可以随着后台配置动态替换图标固然是很方便,体验很好的事情.
动态替换图标使用的是manifest文件配置入口别名activity-alias:
配置文件:





    
        
            
                

                
            
        
        
            
                

                
            
        
    




代码部分:


public class MainActivity extends AppCompatActivity {

    private ComponentName componentName;
    private ComponentName componentNameDefault;
    private PackageManager packageManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取组件别名
        componentName = new ComponentName(getBaseContext(), getPackageName() + ".test_activity");
        componentNameDefault = new ComponentName(getBaseContext(), getClass().getName());
        packageManager = getPackageManager();
    }

    /**
     *
     * 切换状态
     * @param view
     */
    public void mclick(View view){
        if (packageManager.getComponentEnabledSetting(componentName)==PackageManager.COMPONENT_ENABLED_STATE_DISABLED || packageManager.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT){
            packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
            packageManager.setComponentEnabledSetting(componentNameDefault, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
        }else if (packageManager.getComponentEnabledSetting(componentName)==PackageManager.COMPONENT_ENABLED_STATE_ENABLED){
            packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
            packageManager.setComponentEnabledSetting(componentNameDefault, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
        }
    }
}



你可能感兴趣的:(Android 动态替换手机桌面图标)