使用活动别名动态更改应用程序图标

huangapple go评论84阅读模式
英文:

Changing the application icon dynamically with activity alias

问题

在API中有两个图标,我希望当按下按钮1时,应用程序图标是API中的第一个图标,而当按下按钮2时,应用程序图标是API中的第二个图标。我该如何实现这个?

我无法使用API实现这一点,但我设法保存了本地图标并使用别名来使用它们。

<activity-alias
    android:name="OneLauncherAlias"
    android:targetActivity=".MainActivity"
    android:icon="@mipmap/ic_launcher_verisoft1"
    android:label="@string/verisoft_name"
    android:enabled="true"
    android:exported="true">

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

I have two icons in the API, and I want the application icon to be the 1st icon from the API when Button 1 is pressed, and the 2nd icon from the API when Button 2 is pressed. How can I achieve this?

I couldn't achieve it using the API, but I managed to save the local icons and use them with aliases.

&lt;activity-alias
            android:name=&quot;OneLauncherAlias&quot;
            android:targetActivity=&quot;.MainActivity&quot;
            android:icon=&quot;@mipmap/ic_launcher_verisoft1&quot;
            android:label=&quot;@string/verisoft_name&quot;
            android:enabled=&quot;true&quot;
            android:exported=&quot;true&quot;&gt;

            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot;&gt;&lt;/action&gt;
                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;&gt;&lt;/category&gt;
            &lt;/intent-filter&gt;
        &lt;/activity-alias&gt;

答案1

得分: 2

你可以按照以下方法动态更改应用程序图标和名称。

<activity-alias
    android:name=".SplashActivityAlias1"
    android:enabled="false"
    android:exported="true"
    android:icon="@mipmap/ic_launcher_1"
    android:label="计算器"
    android:roundIcon="@mipmap/ic_launcher_1"
    android:targetActivity=".SplashActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity-alias>
<activity-alias
    android:name=".SplashActivityAlias2"
    android:enabled="false"
    android:exported="true"
    android:icon="@mipmap/ic_launcher_2"
    android:label="计算器"
    android:roundIcon="@mipmap/ic_launcher_2"
    android:targetActivity=".SplashActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

你可以通过以下方法为启动器活动添加多个别名,以更改多个图标。就像这样。

val pm = packageManager
pm.setComponentEnabledSetting(
    ComponentName(
        this,
        "com.photovideolocker.SplashActivity"
    ),
    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
    PackageManager.DONT_KILL_APP
)
pm.setComponentEnabledSetting(
    ComponentName(
        this,
        "com.photovideolocker.SplashActivityAlias1"
    ),
    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
    PackageManager.DONT_KILL_APP
)
pm.setComponentEnabledSetting(
    ComponentName(
        this,
        "com.photovideolocker.SplashActivityAlias2"
    ),
    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
    PackageManager.DONT_KILL_APP
)

你可以通过这种方法启用和禁用要设置的图标。

英文:

You can change the App Icon and Name dynamically by following method.

 &lt;activity-alias
            android:name=&quot;.SplashActivityAlias1&quot;
            android:enabled=&quot;false&quot;
            android:exported=&quot;true&quot;
            android:icon=&quot;@mipmap/ic_launcher_1&quot;
            android:label=&quot;Calculator&quot;
            android:roundIcon=&quot;@mipmap/ic_launcher_1&quot;
            android:targetActivity=&quot;.SplashActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity-alias&gt;
        &lt;activity-alias
            android:name=&quot;.SplashActivityAlias2&quot;
            android:enabled=&quot;false&quot;
            android:exported=&quot;true&quot;
            android:icon=&quot;@mipmap/ic_launcher_2&quot;
            android:label=&quot;Calculator&quot;
            android:roundIcon=&quot;@mipmap/ic_launcher_2&quot;
            android:targetActivity=&quot;.SplashActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity-alias&gt;

You have to add Multiple Alias for Laucher activity as many Icons you want to change. Like this.

val pm = packageManager
                            pm.setComponentEnabledSetting(
                                ComponentName(
                                    this,
                                    &quot;com.photovideolocker.SplashActivity&quot;
                                ),
                                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                                PackageManager.DONT_KILL_APP
                            )
                            pm.setComponentEnabledSetting(
                                ComponentName(
                                    this,
                                    &quot;com.photovideolocker.SplashActivityAlias1&quot;
                                ),
                                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                                PackageManager.DONT_KILL_APP
                            )
                            pm.setComponentEnabledSetting(
                                ComponentName(
                                    this,
                                    &quot;com.photovideolocker.SplashActivityAlias2&quot;
                                ),
                                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                                PackageManager.DONT_KILL_APP
                            )

You can enable and disable the icon you want to set by this method.

huangapple
  • 本文由 发表于 2023年8月9日 16:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865844-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定