当前位置:   article > 正文

【Android】动态更新Icon

android:不同样式的动态刷新icon

动态更改图标主要用到activity-aliasPackageManager的setComponentEnabledSetting方法。具体步骤如下:

1、在AndroidManifest.xml中增加以下声明:

  1. <activity-alias
  2. android:name=".alias"
  3. android:enabled="false"
  4. android:icon="@mipmap/icon"
  5. android:label="Alias"
  6. android:targetActivity=".MainActivity">
  7. <intent-filter>
  8. <action android:name="android.intent.action.MAIN" />
  9. <category android:name="android.intent.category.LAUNCHER" />
  10. </intent-filter>
  11. </activity-alias>

完整文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.wangyz.dynamicicon">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. <activity-alias
  18. android:name=".alias"
  19. android:enabled="false"
  20. android:icon="@mipmap/icon"
  21. android:label="Alias"
  22. android:targetActivity=".MainActivity">
  23. <intent-filter>
  24. <action android:name="android.intent.action.MAIN" />
  25. <category android:name="android.intent.category.LAUNCHER" />
  26. </intent-filter>
  27. </activity-alias>
  28. </application>
  29. </manifest>

2、切换图标

  1. getPackageManager().setComponentEnabledSetting(getComponentName(),
  2. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  3. PackageManager.DONT_KILL_APP);
  4. getPackageManager().setComponentEnabledSetting(new ComponentName(this,
  5. getPackageName() + ".alias"),
  6. PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  7. 0);

setComponentEnabledSetting方法的最后一个参数,PackageManager.DONT_KILL_APP表示不关闭App,0表示关闭App刷新图标。

完整文件如下:

  1. package com.wangyz.dynamicicon;
  2. import android.content.ComponentName;
  3. import android.content.pm.PackageManager;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. public class MainActivity extends AppCompatActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. }
  13. public void changeIcon(View view) {
  14. getPackageManager().setComponentEnabledSetting(getComponentName(),
  15. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  16. PackageManager.DONT_KILL_APP);
  17. getPackageManager().setComponentEnabledSetting(new ComponentName(this,
  18. getPackageName() + ".MainActivity"),
  19. PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  20. 0);
  21. }
  22. public void changeIconAlias(View view) {
  23. getPackageManager().setComponentEnabledSetting(getComponentName(),
  24. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  25. PackageManager.DONT_KILL_APP);
  26. getPackageManager().setComponentEnabledSetting(new ComponentName(this,
  27. getPackageName() + ".alias"),
  28. PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  29. 0);
  30. }
  31. }

源码地址:https://github.com/milovetingting/Samples/tree/master/DynamicIcon

转载于:https://www.cnblogs.com/milovetingting/p/10436746.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/865729
推荐阅读
相关标签
  

闽ICP备14008679号