How to change the icon and label of an app once it is installed?

 ̄綄美尐妖づ 提交于 2019-11-28 04:39:36

问题


I am trying to change the icon and label of my app once it is installed.

In the manifest, i put this code :

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme" >
// 1st Activity declaration
<activity
    android:name=".activities.SplashScreenActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.Sherlock.NoActionBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        // I removed the next line to put it with the alias :
        // <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

// Activity Aliases
<activity-alias
    android:name=".Alias_0"
    android:enabled="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:targetActivity=".activities.SplashScreenActivity" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity-alias>
<activity-alias
    android:name=".Alias_1"
    android:enabled="false"
    android:icon="@drawable/alias1"
    android:label="@string/alias1"
    android:targetActivity=".activities.SplashScreenActivity" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity-alias>

And in a PreferenceActivity, I am enabling/disabling aliases like this :

packageManager.setComponentEnabledSetting(
        new ComponentName("com.mypackage", "com.mypackage.Alias_0"),
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
        PackageManager.DONT_KILL_APP);

packageManager.setComponentEnabledSetting(
        new ComponentName("com.mypackage", "com.mypackage.Alias_1"),
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP);

There is always only one activated alias.

Once this is changed, I can see on the application launcher that the old app icon/label has disappeared and on some devices, it takes some times (it can last very long) for the launcher to display the activated one. During this time, there is no way to launch the app since there is no icon.

Is there any way to refresh the launcher programmatically ?

Also, i've tried to create a shortcut after the activation/deactivation of the aliases. It works fine but if I try to move the shortcut around, once I release the shortcut to its new position, the Android launcher crashes. Here is the code I'm using :

Intent shortcutIntent = new Intent(getApplicationContext(), SplashScreenActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutTitle);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), shortcutDrawableId));
addIntent.putExtra("duplicate", false);

addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(addIntent);

Am I missing something ?

EDIT : Just to be clear, I am not looking to change the name of my app BEFORE installing it. I want to do it once the app is installed on my phone.

I want to give the opportunity to users to have another icon/label once they have installed my app. If they don't activate that option, they should have the original icon/label.

I am also not looking to change the name of the app with an update.


回答1:


The code posted in the question is apparently the correct way to do it. There is an issue with Android 4.1 and 4.2. Find more details here:

https://code.google.com/p/android/issues/detail?id=54546




回答2:


I think I may be able to help you. Under the manifest tag there is this:

<application
    android:allowBackup="true"
    android:icon="@drawable/launcher_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" />

You can edit these and change it to the icon that you saved in your drawables for your launcher and you can also change them name to a different string.




回答3:


To change the name of your application right click the application folder in the package explorer, look for "Refactor" and press rename.

To change the icon of your application you have to change all of its variations inside the drawable folders and also the "ic_launcher-web.png" inside your res folder.



来源:https://stackoverflow.com/questions/18004230/how-to-change-the-icon-and-label-of-an-app-once-it-is-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!