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

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

Changing the application icon dynamically with activity alias

问题

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

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

  1. <activity-alias
  2. android:name="OneLauncherAlias"
  3. android:targetActivity=".MainActivity"
  4. android:icon="@mipmap/ic_launcher_verisoft1"
  5. android:label="@string/verisoft_name"
  6. android:enabled="true"
  7. android:exported="true">
  8. <intent-filter>
  9. <action android:name="android.intent.action.MAIN"></action>
  10. <category android:name="android.intent.category.LAUNCHER"></category>
  11. </intent-filter>
  12. </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.

  1. &lt;activity-alias
  2. android:name=&quot;OneLauncherAlias&quot;
  3. android:targetActivity=&quot;.MainActivity&quot;
  4. android:icon=&quot;@mipmap/ic_launcher_verisoft1&quot;
  5. android:label=&quot;@string/verisoft_name&quot;
  6. android:enabled=&quot;true&quot;
  7. android:exported=&quot;true&quot;&gt;
  8. &lt;intent-filter&gt;
  9. &lt;action android:name=&quot;android.intent.action.MAIN&quot;&gt;&lt;/action&gt;
  10. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;&gt;&lt;/category&gt;
  11. &lt;/intent-filter&gt;
  12. &lt;/activity-alias&gt;

答案1

得分: 2

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

  1. <activity-alias
  2. android:name=".SplashActivityAlias1"
  3. android:enabled="false"
  4. android:exported="true"
  5. android:icon="@mipmap/ic_launcher_1"
  6. android:label="计算器"
  7. android:roundIcon="@mipmap/ic_launcher_1"
  8. android:targetActivity=".SplashActivity">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity-alias>
  14. <activity-alias
  15. android:name=".SplashActivityAlias2"
  16. android:enabled="false"
  17. android:exported="true"
  18. android:icon="@mipmap/ic_launcher_2"
  19. android:label="计算器"
  20. android:roundIcon="@mipmap/ic_launcher_2"
  21. android:targetActivity=".SplashActivity">
  22. <intent-filter>
  23. <action android:name="android.intent.action.MAIN" />
  24. <category android:name="android.intent.category.LAUNCHER" />
  25. </intent-filter>
  26. </activity-alias>

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

  1. val pm = packageManager
  2. pm.setComponentEnabledSetting(
  3. ComponentName(
  4. this,
  5. "com.photovideolocker.SplashActivity"
  6. ),
  7. PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  8. PackageManager.DONT_KILL_APP
  9. )
  10. pm.setComponentEnabledSetting(
  11. ComponentName(
  12. this,
  13. "com.photovideolocker.SplashActivityAlias1"
  14. ),
  15. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  16. PackageManager.DONT_KILL_APP
  17. )
  18. pm.setComponentEnabledSetting(
  19. ComponentName(
  20. this,
  21. "com.photovideolocker.SplashActivityAlias2"
  22. ),
  23. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  24. PackageManager.DONT_KILL_APP
  25. )

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

英文:

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

  1. &lt;activity-alias
  2. android:name=&quot;.SplashActivityAlias1&quot;
  3. android:enabled=&quot;false&quot;
  4. android:exported=&quot;true&quot;
  5. android:icon=&quot;@mipmap/ic_launcher_1&quot;
  6. android:label=&quot;Calculator&quot;
  7. android:roundIcon=&quot;@mipmap/ic_launcher_1&quot;
  8. android:targetActivity=&quot;.SplashActivity&quot;&gt;
  9. &lt;intent-filter&gt;
  10. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  11. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  12. &lt;/intent-filter&gt;
  13. &lt;/activity-alias&gt;
  14. &lt;activity-alias
  15. android:name=&quot;.SplashActivityAlias2&quot;
  16. android:enabled=&quot;false&quot;
  17. android:exported=&quot;true&quot;
  18. android:icon=&quot;@mipmap/ic_launcher_2&quot;
  19. android:label=&quot;Calculator&quot;
  20. android:roundIcon=&quot;@mipmap/ic_launcher_2&quot;
  21. android:targetActivity=&quot;.SplashActivity&quot;&gt;
  22. &lt;intent-filter&gt;
  23. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  24. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  25. &lt;/intent-filter&gt;
  26. &lt;/activity-alias&gt;

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

  1. val pm = packageManager
  2. pm.setComponentEnabledSetting(
  3. ComponentName(
  4. this,
  5. &quot;com.photovideolocker.SplashActivity&quot;
  6. ),
  7. PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  8. PackageManager.DONT_KILL_APP
  9. )
  10. pm.setComponentEnabledSetting(
  11. ComponentName(
  12. this,
  13. &quot;com.photovideolocker.SplashActivityAlias1&quot;
  14. ),
  15. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  16. PackageManager.DONT_KILL_APP
  17. )
  18. pm.setComponentEnabledSetting(
  19. ComponentName(
  20. this,
  21. &quot;com.photovideolocker.SplashActivityAlias2&quot;
  22. ),
  23. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  24. PackageManager.DONT_KILL_APP
  25. )

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:

确定