简体   繁体   中英

Can we programatically change android:icon associated with activity tag in the manifest?

如何以编程方式更改与android清单中的活动标记关联的android:icon

You cannot change the manifest or the resource in the signed-and-sealed APK, except through a software upgrade.

Also, According to this the default's app icon is set in the manifest. As manifest is located in the app's root directory it cannot be modified, so there's no way to do it. You may consider that if you could change it while the app is running, changes wouldn't be saved in the manifest.

While the icon is a compile time setting you can change the app icon using an Activity Alias.

<activity-alias
    android:name=".MainActivity-IconA"
    android:enabled="true"
    android:icon="@mipmap/icon_a"
    android:label="@string/icon_a_label"
    android:exported="true"
    android:targetActivity=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity-alias>

So you could have multiple of Alias' with different icons. You then just enable/disable each one at run time:

PackageManager pm = context.getPackageManager();

// Disable Icon A
pm.setComponentEnabledSetting(
    new ComponentName(this, "com.example.MainActivity-IconA"),
    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
    PackageManager.DONT_KILL_APP
);

// Enable Icon B
pm.setComponentEnabledSetting(
    new ComponentName(this, "com.example.MainActivity-IconB"),
    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
    PackageManager.DONT_KILL_APP
);

This of course means that you know what your icons are going to be a build time too, but at least you can switch between them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM