How to implement event listeners on app shortcut icon?

Viewed 865

I would like to create an app that deals with background pictures of the a user device. But when a user clicks the shortcut icon, the background should change without opening the app and the icon should be somehow animated.

Let's consider this application in Itel:

Before I click on the app shortcut icon enter image description here

And after click, the app doesn't open but the background changes and the icon animates(see the picture):

enter image description here

How can someone achieve this?

2 Answers
  1. AFAIK we cannot change app icon at runtime. My guess, The icon in the example above may be a widget

  2. About changing background wallpaper (or performing a particular task): I think we can create a launcher activity (a transparent one/without setContentView()) which will finish() itself after triggering a background service that changes the wallpaper (or perform any other task). According to my opinion, this can be a solution for the scenario above though I haven't tried it personally

Best regards, Happy coding :)

tl;dr You can't change it because the icon of your app is registered in the Android Manifest that ships with it.

From the documentation:

Icons and labels

A number of manifest elements have an icon and label attributes for displaying a small icon and a text label, respectively, to users for the corresponding app component.

That means that your app will always have the same icon since the manifest cannot be changed in runtime. So my guess is that the app you reference is a system app, with system privileges.

The only icons that you can change are the shortcuts your app creates using this permission:

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

To create a shortcut, please check this answer: https://stackoverflow.com/a/40446734/1574250


To change the background when clicking in your app icon, here is an example (in this example I'm only changing the background color when the app opens):

Class:

public class YourActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set you app icon
        setColorWallpaper();

        // Finish the activity
        finish();
    }

    /**
     * Sets the color wallpaper to the color value in the Clipboard, or to a random color.
     */
    private void setColorWallpaper() {

        // Try to get the color parameter from the clipboard
        Integer colorParam = null;
        try {
            colorParam = ColorClipboardParameter.getColor(getApplication());
        } catch (Exception ignored) {
            // An unexpected exception while trying to get the color code from the clipboard
            // can crash the app at startup. Ignore any exceptions, we will generate a random
            // color anyway.
        }

        // If there is no valid color value in the clipboard, generate a random color
        final int color = (colorParam != null) ? colorParam : GoodRandomColor.nextColor();

        try {
            // Set the color wallpaper
            ColorWallpaper.setColorWallpaper(this, color);

            // Success: copy the color code to the clipboard
            Utils.copyText(this, Utils.colorToHex(color));

            // Go to the home screen
            Utils.goHome(this);

        } catch (IOException e) {

            // Write the stack trace to System.err and copy the reason of the failure to clipboard
            e.printStackTrace();
            Utils.copyText(this, e.toString());
        }
    }
}

Manifest:

<application
    android:fullBackupContent="true"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:installLocation="auto"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">

    <activity
        android:name=".YourActivity"
        android:excludeFromRecents="true"
        android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

    </activity>

</application>

Check this project for more info: https://github.com/appgramming/LoneColor-Android

Related