現(xiàn)在淘寶,京東等App在節(jié)假日打開(kāi)時(shí),都是采用一套節(jié)假日的圖標(biāo),這種不用發(fā)版本,又可以隨著后臺(tái)配置動(dòng)態(tài)替換圖標(biāo)固然是很方便,體驗(yàn)很好的事情.
動(dòng)態(tài)替換圖標(biāo)使用的是manifest文件配置入口別名activity-alias:
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mychangeicondome">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:icon="@mipmap/ic_launcher"
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity-alias
android:icon="@mipmap/ic_launcher"
android:name=".test_activity"
android:enabled="false"
android:label="OtherName"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
</application>
</manifest>
代碼部分:
public class MainActivity extends AppCompatActivity {
private ComponentName componentName;
private ComponentName componentNameDefault;
private PackageManager packageManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取組件別名
componentName = new ComponentName(getBaseContext(), getPackageName() + ".test_activity");
componentNameDefault = new ComponentName(getBaseContext(), getClass().getName());
packageManager = getPackageManager();
}
/**
*
* 切換狀態(tài)
* @param view
*/
public void mclick(View view){
if (packageManager.getComponentEnabledSetting(componentName)==PackageManager.COMPONENT_ENABLED_STATE_DISABLED || packageManager.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT){
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
packageManager.setComponentEnabledSetting(componentNameDefault, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
}else if (packageManager.getComponentEnabledSetting(componentName)==PackageManager.COMPONENT_ENABLED_STATE_ENABLED){
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
packageManager.setComponentEnabledSetting(componentNameDefault, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
}
}
}