Icon and the application name dynamically modify the application's AndroidManifest.xml

A, activity-alias tag

Android app name and icon support dynamic modification of the application. 12 11 pairs such as two-time Taobao and Jingdong APP without the need for updates automatically change the icon, so when using dynamic updating of application icons and names the best scenario projects like electricity supplier promotions.
The reason why Android supports dynamic update icon because there is a activity-alias tag in AndroidManifest.xml, from the label name will be able to see that this activity is an alias. Tell me introduce our network,
https://developer.android.com/guide/topics/manifest/activity-alias-element.html

An alias for an activity, named by the targetActivity attribute. The target must be in the same application as the alias and it must be declared before the alias in the manifest. 

Roughly means: This is the property targetActivity: the activity of an alias, this property targetActivity: the activity must be in the same application, and targetActivity must be defined with this alias in the Alias ​​money.

Let's look at this activity-alias tag has those attributes

    <activity-alias android:enabled=["true" | "false"]
                    android:exported=["true" | "false"]
                    android:icon="drawable resource"
                    android:label="string resource"
                    android:name="string"
                    android:permission="string"
                    android:targetActivity="string" >
        . . .
    </activity-alias>

Many property related activity consistent tag attributes, targetActivity attribute mainly to see the definition of the time mentioned, the value of this property is that you want to use an alias instead of that activity, and this activity must be defined before this activity-alias. Here say that, if we want a dynamic replacement icon and name change, it must be stated `

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>` 这个启动activity的别名,在别名里面修改。

Second, the dynamic modification icon and name

1, set the launch activity alias:

<activity android:name=".MainActivity"
            android:icon="@mipmap/ic_launcher"
            >

            <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity-alias
            android:name=".Test"
            android:enabled="false"
            android:icon="@mipmap/launch_01"
            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>

Here mainactivity set up an alias, modification icon and label attributes in activity-alias, the modified icons and names you want to replace, the name is an alias name, the activity is activity in the class name. You must not be missing android:targetActivity=".MainActivity"property.

2, where the operation needs to be replaced

首先我在布局文件中activity_main.xml定义一个简单的按钮,当点击的时候更换图标。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.pursue.it.iconchange.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改图标"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>

In mainactivity in:

package com.pursue.it.iconchange;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeIcon();
            }
        });
    }


//修改图标和名称的方法
    private void changeIcon() {
        PackageManager pm = getApplicationContext().getPackageManager();
        System.out.println(getComponentName());
       //去掉旧图标
        pm.setComponentEnabledSetting(getComponentName(),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        //显示新图标
        pm.setComponentEnabledSetting(new ComponentName(
                        getBaseContext(),
                        "com.pursue.it.iconchange.Test"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }
}

Click the button wait a few seconds, you can see the phone's desktop icon and the name is automatically replaced.
The actual development, should be based on the results of a network request to trigger changeIcon () method to replace the icon.
demo Address: http://download.csdn.net/download/sushineboy/10145540

Published 29 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/sushineboy/article/details/78711546