如何在应用快捷方式图标上实现事件监听器?

时间:2018-10-24 21:37:10

标签: android icons

我想创建一个处理用户设备背景图片的应用。但是,当用户单击快捷方式图标时,背景应更改而无需打开应用程序,并且图标应以某种方式进行动画处理。

让我们在Itel中考虑此应用程序

在我单击应用程序快捷方式图标之前 enter image description here

点击后,该应用无法打开,但背景发生了变化,并且该图标具有动画效果(见图片):

enter image description here

有人可以做到这一点吗?

2 个答案:

答案 0 :(得分:3)

  1. 我们不能在运行时更改应用程序图标。我的猜测是,上面示例中的图标可能是小部件

  2. 关于更改背景墙纸(或执行特定任务):我认为我们可以创建启动器活动(透明的活动/无setContentView(),它将{{1 }}本身,然后触发了更改墙纸(或执行任何其他任务)的后台服务。根据我的观点,尽管我没有亲自尝试过,但这可能是上述情况的解决方案

最诚挚的问候,祝您编码愉快:)

答案 1 :(得分:2)

tl; dr 您无法更改它,因为您的应用程序图标已在其附带的Android清单中注册。

来自documentation

  

图标和标签

     

许多清单元素具有图标和标签属性   向用户分别显示一个小图标和一个文本标签   相应的应用程序组件。

这意味着您的应用程序将始终具有相同的图标,因为清单文件无法在运行时更改。因此,我的猜测是您引用的应用程序是具有系统特权的系统应用程序。

您唯一可以更改的图标是应用使用此权限创建的快捷方式:

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

要创建快捷方式,请检查以下答案:https://stackoverflow.com/a/40446734/1574250


要在单击应用程序图标时更改背景,下面是一个示例(在此示例中,我仅在应用程序打开时更改背景色):

班级

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());
        }
    }
}

清单:

<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>

检查该项目以获取更多信息:https://github.com/appgramming/LoneColor-Android