How to change an application icon programmatically in Android?

前端 未结 10 2165
闹比i
闹比i 2020-11-21 22:33

Is it possible to change an application icon directly from the program?
I mean, change icon.png in the res\\drawable folder.
I would like t

10条回答
  •  后悔当初
    2020-11-21 23:20

    @P-A's solution partially works for me. Detail my findings below:

    1) The first code snippet is incorrect, see below:

    
            ==>  <== This line shouldn't be deleted, otherwise will have compile error
             //DELETE THIS LINE
        
    
    

    2) Should use following code to disable all icons before enabling another one, otherwise it will add a new icon, instead of replacing it.

    getPackageManager().setComponentEnabledSetting(
            getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    

    BUT, if you use code above, then shortcut on homescreen will be removed! And it won't be automatically added back. You might be able to programmatically add icon back, but it probably won't stay in the same position as before.

    3) Note that the icon won't get changed immediately, it might take several seconds. If you click it right after changing, you might get an error saying: "App isn't installed".

    So, IMHO this solution is only suitable for changing icon in app launcher only, not for shortcuts (i.e. the icon on homescreen)

提交回复
热议问题