How to change an application icon programmatically in Android?

前端 未结 10 2174
闹比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:07

    Programatically, you may want to publish the application launcher yourself :

    Note: this method no longer works starting with Android 8.0 - Oreo

    In your AndroidManifest.xml, add :

    
    

    Then you need create your app launcher intent:

    Intent myLauncherIntent = new Intent();
    myLauncherIntent.setClassName("your.package.name", "YourLauncherActivityName");
    myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    

    Create an install shortcut intent with your app launcher and custom icon:

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Application Name");
    intent.putExtra
           (
            Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext
                                        (
                                             getApplicationContext(), 
                                             R.drawable.app_icon
                                        )
           );
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    

    And finally launch the broadcast intent:

    getApplicationContext().sendBroadcast(intent);
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题