Android 动态替换手机桌面图标

2017-04-12  本文已影响0人  一航jason

现在淘宝,京东等App在节假日打开时,都是采用一套节假日的图标,这种不用发版本,又可以随着后台配置动态替换图标固然是很方便,体验很好的事情.
动态替换图标使用的是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();
    }

    /**
     *
     * 切换状态
     * @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);
        }
    }
}



上一篇下一篇

猜你喜欢

热点阅读