美文网首页
动态更改桌面app图标

动态更改桌面app图标

作者: _蘇芳_ | 来源:发表于2017-01-19 17:53 被阅读149次

    title: 动态更改桌面app图标
    date: 2016-12-13 10:33:27
    tags: Utils


    • 清单中<application>里的设置

      <activity android:name=".MainActivity">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN"/>
                  </intent-filter>
       </activity>
       <activity-alias
                  android:name=".maoyu"
                  android:enabled="false"
                  android:icon="@mipmap/maoyu"
                  android:label="maoyu"
                  android:targetActivity=".MainActivity">
                    <intent-filter>
                      <action android:name="android.intent.action.MAIN"/>
                      <category android:name="android.intent.category.LAUNCHER"/>
                  </intent-filter>
        </activity-alias>
        <activity-alias
                  android:name=".uu"
                  android:enabled="true"
                  android:icon="@mipmap/uu"
                  android:label="uu"
                  android:targetActivity=".MainActivity">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN"/>
                      <category android:name="android.intent.category.LAUNCHER"/>
                  </intent-filter>
        </activity-alias>
      

      将原先的activity标签下的<category android:name="android.intent.category.LAUNCHER"/>删掉

      添加`<activity-alias>标签

      android:name=".uu" 随便起的名字

      android:enabled="true" true表示默认就使用这个,桌面图标就是这个,应用名就是上面那个name

      android:icon="@mipmap/uu" 下面两个不用解释了

      android:targetActivity=".MainActivity">

      上代码

      private void switchIcon(boolean isUU) {
              try {
                  //要跟manifest的activity-alias 的name保持一致  
                  String maoyu = "com.aidebar.demo.maoyu";
                  String uu = "com.aidebar.demo.uu";
      
                  PackageManager pm = getPackageManager();
                  ComponentName uuComponentName = new ComponentName(getApplicationContext(), uu);
                  int uuState = isUU ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                          : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
                  //如果是UU,那么设置成enabled
                  if (pm.getComponentEnabledSetting(uuComponentName) != uuState) {
                      pm.setComponentEnabledSetting(uuComponentName, uuState,        PackageManager.DONT_KILL_APP);
                  }
                  
                  ComponentName maoyuComponentName = new ComponentName(getApplicationContext(), maoyu);
                  //如果是UU,其他的都要设为disabled
                  int maoyuState = !isUU ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                          : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
                  if (pm.getComponentEnabledSetting(maoyuComponentName) != maoyuState) {//新状态跟当前状态不一样才执行  
                      pm.setComponentEnabledSetting(maoyuComponentName, maoyuState, PackageManager.DONT_KILL_APP);
                  }
              } catch (Exception e){
              }
          }
      

      可以在每次打开的时候判断下日期,如果是双十二啦,圣诞啦,就可以调用这方法,来改变应用图标啦,但是要注意试了几个测试机,都要十来秒才会有变化,在这十秒内点击桌面图标会出现该应用未正确安装的提示

    相关文章

      网友评论

          本文标题:动态更改桌面app图标

          本文链接:https://www.haomeiwen.com/subject/gviibttx.html