📌  相关文章
📜  如何在Android中以编程方式更改Android的应用程序图标?

📅  最后修改于: 2022-05-13 01:55:37.770000             🧑  作者: Mango

如何在Android中以编程方式更改Android的应用程序图标?

在本文中,我们将学习如何在按钮单击时更改应用程序的应用程序图标。当我们有针对不同类型用户的应用程序时,可以使用此功能。然后根据用户类型,我们可以动态更改应用程序图标。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。

第 2 步:在 AndroidManifest.xml 文件中添加它

XML

  

  

  

  
    
  
        
        
  
    
  

  

  
    
  
        
        
  
    
  
        
    


XML


  
    
    
    
  


Java
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView click,newicon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click=findViewById(R.id.oldicon);
        newicon=findViewById(R.id.newicon);
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeicon();
            }
        });
        newicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newicon();
            }
        });
  
    }
    private void changeicon() {
          
          // enable old icon
        PackageManager manager=getPackageManager();
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivity")
                ,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
          
          // disable new icon
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivityAlias")
                ,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
        Toast.makeText(MainActivity.this,"Enable Old Icon" ,Toast.LENGTH_LONG).show();
    }
  
    private void newicon() {
          
          // enable old icon
        PackageManager manager=getPackageManager();
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivity")
                ,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
          
          // enable new icon
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivityAlias")
                ,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
        Toast.makeText(MainActivity.this,"Enable New Icon" ,Toast.LENGTH_LONG).show();
    }
}


步骤 3:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。

XML



  
    
    
    
  

第 4 步:使用 MainActivity。 Java文件

转到主活动。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView click,newicon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click=findViewById(R.id.oldicon);
        newicon=findViewById(R.id.newicon);
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeicon();
            }
        });
        newicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newicon();
            }
        });
  
    }
    private void changeicon() {
          
          // enable old icon
        PackageManager manager=getPackageManager();
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivity")
                ,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
          
          // disable new icon
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivityAlias")
                ,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
        Toast.makeText(MainActivity.this,"Enable Old Icon" ,Toast.LENGTH_LONG).show();
    }
  
    private void newicon() {
          
          // enable old icon
        PackageManager manager=getPackageManager();
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivity")
                ,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
          
          // enable new icon
        manager.setComponentEnabledSetting(new ComponentName(MainActivity.this,"com.prepare.makedirectory.MainActivityAlias")
                ,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
        Toast.makeText(MainActivity.this,"Enable New Icon" ,Toast.LENGTH_LONG).show();
    }
}

输出: