Android动态修改应用图标

最近遇到一个功能模块化管理APP, 需要根据不同的业务动态替换应用图标和启动页,搜了大量资料,总结用”别名”activity-alias标签比较实用 简单

效果视频 http://m.v.qq.com/play.html?cid=&vid=d32368cv2hl&vuid24=MwW8NfoGvVm7RsR2bKr7pw%3D%3D&url_from=share&second_share=0&share_from=wxf

直接上代码

布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main18Activity">
<Button
    android:id="@+id/a1"
    android:text="北京"
    android:onClick="monClickq"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</Button>
    <Button
        android:text="上海"
        android:id="@+id/a2"
        android:onClick="monClickw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Button>
    <Button
        android:text="广东"
        android:id="@+id/a3"
        android:onClick="monClicke"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Button>
</LinearLayout>

AndroidManifest.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:name=".HuodeApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ss"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"

        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity-alias
            android:name=".Main2Activity"
            android:enabled="false"
            android:icon="@mipmap/ic_launcher_round"
            android:label="北京"
            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=".Main3Activity"
            android:enabled="false"
            android:icon="@mipmap/qa_launcher"
            android:label="上海"
            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>
MainActivity 代码
public class Main18Activity extends AppCompatActivity {
    private ComponentName mComponentName1;

    private ComponentName mComponentName2;

    private ComponentName mComponentNameDefault3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main18);
        String packageName = getPackageName();
        mComponentName1 = new ComponentName(this, packageName + ".MainActivity");
        mComponentName2 = new ComponentName(this, packageName + ".Main2Activity");
        mComponentNameDefault3 = new ComponentName(this, packageName + ".Main3Activity");

    }


    private void enableComponent(ComponentName componentName) {
        getPackageManager().setComponentEnabledSetting(componentName,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    private void disableComponent(ComponentName componentName) {
        getPackageManager().setComponentEnabledSetting(componentName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

    public void monClick(View view) {
        enableComponent(mComponentName1);
        disableComponent(mComponentName2);
        disableComponent(mComponentNameDefault3);
    }

    public void monClickq(View view) {
        enableComponent(mComponentName1);
        disableComponent(mComponentName2);
        disableComponent(mComponentNameDefault3);
    }

    public void monClickw(View view) {
        disableComponent(mComponentName1);
        enableComponent(mComponentName2);
        disableComponent(mComponentNameDefault3);
    }

    public void monClicke(View view) {
        disableComponent(mComponentName1);
        disableComponent(mComponentName2);
        enableComponent(mComponentNameDefault3);
    }
}

注意: AndroidManifest.xml  android:enabled=”false” false表示只会显示一个图标   true表示显示同时存在

现在就可以根绝不同业务活动等需求调用不同的 启动页 APP名字  图标 

 

 

 

    原文作者:软件熊猫
    原文地址: https://blog.csdn.net/Originality_special/article/details/115207619
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞