How to change an application icon programmatically in Android?

前端 未结 10 2135
闹比i
闹比i 2020-11-21 22:33

Is it possible to change an application icon directly from the program?
I mean, change icon.png in the res\\drawable folder.
I would like t

相关标签:
10条回答
  • 2020-11-21 22:55

    Try this solution

    <activity android:name=".SplashActivity"
            android:label="@string/app_name"
            android:icon="@drawable/ic_launcher">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <activity-alias android:label="ShortCut"
            android:icon="@drawable/ic_short_cut"
            android:name=".SplashActivityAlias"
            android:enabled="false"
            android:targetActivity=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>
    

    Add the following code when you want to change your app icon

    PackageManager pm = getPackageManager();
                        pm.setComponentEnabledSetting(
                                new ComponentName(YourActivity.this,
                                        "your_package_name.SplashActivity"),
                                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                                PackageManager.DONT_KILL_APP);
    
                        pm.setComponentEnabledSetting(
                                new ComponentName(YourActivity.this,
                                        "your_package_name.SplashActivityAlias"),
                                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                                PackageManager.DONT_KILL_APP);
    
    0 讨论(0)
  • 2020-11-21 22:57

    Assuming you mean changing the icon shown on the home screen, this could easily be done by creating a widget that does exactly this. Here's an article that demonstrate how that can be accomplished for a "new messages" type application similar to iPhone:

    http://www.cnet.com/8301-19736_1-10278814-251.html

    0 讨论(0)
  • 2020-11-21 23:04

    It's an old question, but still active as there is no explicit Android feature. And the guys from facebook found a work around - somehow. Today, I found a way that works for me. Not perfect (see remarks at the end of this answer) but it works!

    Main idea is, that I update the icon of my app's shortcut, created by the launcher on my home screen. When I want to change something on the shortcut-icon, I remove it first and recreate it with a new bitmap.

    Here is the code. It has a button increment. When pressed, the shortcut is replaced with one that has a new counting number.

    First you need these two permissions in your manifest:

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
    

    Then you need this two methods for installing and uninstalling shortcuts. The shortcutAdd method creates a bitmap with a number in it. This is just to demonstrate that it actually changes. You probably want to change that part with something, you want in your app.

    private void shortcutAdd(String name, int number) {
        // Intent to be send, when shortcut is pressed by user ("launched")
        Intent shortcutIntent = new Intent(getApplicationContext(), Play.class);
        shortcutIntent.setAction(Constants.ACTION_PLAY);
    
        // Create bitmap with number in it -> very default. You probably want to give it a more stylish look
        Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        paint.setColor(0xFF808080); // gray
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(50);
        new Canvas(bitmap).drawText(""+number, 50, 50, paint);
        ((ImageView) findViewById(R.id.icon)).setImageBitmap(bitmap);
    
        // Decorate the shortcut
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
    
        // Inform launcher to create shortcut
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }
    
    private void shortcutDel(String name) {
        // Intent to be send, when shortcut is pressed by user ("launched")
        Intent shortcutIntent = new Intent(getApplicationContext(), Play.class);
        shortcutIntent.setAction(Constants.ACTION_PLAY);
    
        // Decorate the shortcut
        Intent delIntent = new Intent();
        delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    
        // Inform launcher to remove shortcut
        delIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(delIntent);
    }
    

    And finally, here are two listener to add the first shortcut and update the shortcut with an incrementing counter.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.test);
        findViewById(R.id.add).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                shortcutAdd("changeIt!", count);
            }
        });
        findViewById(R.id.increment).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                shortcutDel("changeIt!");
                count++;
                shortcutAdd("changeIt!", count);
            }
        });
    }
    

    Remarks:

    • This way works also if your App controls more shortcuts on the home screen, e.g. with different extra's in the Intent. They just need different names so that the right one is uninstalled and reinstalled.

    • The programmatical handling of shortcuts in Android is a well known, widely used but not officially supported Android feature. It seems to work on the default launcher and I never tried it anywhere else. So dont blame me, when you get this user-emails "It does not work on my XYZ, double rooted, super blasted phone"

    • The launcher writes a Toast when a shortcut was installad and one when a shortcut was uninstalled. So I get two Toasts every time I change the icon. This is not perfect, but well, as long as the rest of my app is perfect...

    0 讨论(0)
  • 2020-11-21 23:07

    Programatically, you may want to publish the application launcher yourself :

    Note: this method no longer works starting with Android 8.0 - Oreo

    In your AndroidManifest.xml, add :

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    

    Then you need create your app launcher intent:

    Intent myLauncherIntent = new Intent();
    myLauncherIntent.setClassName("your.package.name", "YourLauncherActivityName");
    myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    

    Create an install shortcut intent with your app launcher and custom icon:

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Application Name");
    intent.putExtra
           (
            Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext
                                        (
                                             getApplicationContext(), 
                                             R.drawable.app_icon
                                        )
           );
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    

    And finally launch the broadcast intent:

    getApplicationContext().sendBroadcast(intent);
    
    0 讨论(0)
  • 2020-11-21 23:09

    You cannot change the manifest or the resource in the signed-and-sealed APK, except through a software upgrade.

    0 讨论(0)
  • 2020-11-21 23:09

    AndroidManifest.xml example:

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name="com.pritesh.resourceidentifierexample.MainActivity"
                      android:label="@string/app_name"
                      android:launchMode="singleTask">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <!--<category android:name="android.intent.category.LAUNCHER"/>-->
                </intent-filter>
            </activity>
    
            <activity-alias android:label="RED"
                            android:icon="@drawable/ic_android_red"
                            android:name="com.pritesh.resourceidentifierexample.MainActivity-Red"
                            android:enabled="true"
                            android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity-alias>
    
            <activity-alias android:label="GREEN"
                            android:icon="@drawable/ic_android_green"
                            android:name="com.pritesh.resourceidentifierexample.MainActivity-Green"
                            android:enabled="false"
                            android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity-alias>
    
            <activity-alias android:label="BLUE"
                            android:icon="@drawable/ic_android_blue"
                            android:name="com.pritesh.resourceidentifierexample.MainActivity-Blue"
                            android:enabled="false"
                            android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity-alias>
    
        </application>
    

    Then follow below given code in MainActivity:

    ImageView imageView = (ImageView)findViewById(R.id.imageView);
                int imageResourceId;
                String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
                int hours = new Time(System.currentTimeMillis()).getHours();
                Log.d("DATE", "onCreate: "  + hours);
    
                getPackageManager().setComponentEnabledSetting(
                        getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    
                if(hours == 13)
                {
                    imageResourceId = this.getResources().getIdentifier("ic_android_red", "drawable", this.getPackageName());
                    getPackageManager().setComponentEnabledSetting(
                            new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Red"),
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                }else if(hours == 14)
                {
                    imageResourceId = this.getResources().getIdentifier("ic_android_green", "drawable", this.getPackageName());
                    getPackageManager().setComponentEnabledSetting(
                            new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Green"),
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    
                }else
                {
                    imageResourceId = this.getResources().getIdentifier("ic_android_blue", "drawable", this.getPackageName());
                    getPackageManager().setComponentEnabledSetting(
                            new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Blue"),
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    
                }
    
                imageView.setImageResource(imageResourceId);
    
    0 讨论(0)
提交回复
热议问题