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

3
我想创建一个处理用户设备背景图片的应用程序。但是当用户点击快捷图标时,背景应该在不打开应用程序的情况下更改,并且图标应该以某种方式进行动画处理。
让我们在Itel中考虑这个应用程序:
在我点击应用程序快捷图标之前: enter image description here 点击后,应用程序不会打开,但是背景会更改并且图标会动画显示(请参见图片): enter image description here 如何实现这一点?

1
这个应用程序是内置在系统中的吗?如果使用自定义应用程序启动器,它的行为是否相同?这可能只是启动器应用程序中硬编码的行为。 - Pawel
你的目标 API 级别是什么? - BLuFeNiX
1
如果您无法对应用程序图标本身进行动画/更改,则可以尝试打开一个完全透明的活动并在那里播放动画,以便看起来像图标已经被动画化了。 - Pezo
@BLuFeNiX 目标 SDK 是 26。 - Gratien Asimbahwe
你可以尝试使用自适应图标 - DWattimena
显示剩余2条评论
2个回答

3
  1. 据我所知,我们无法在运行时更改应用程序图标。我猜想,上面的示例中的图标可能是一个小部件。

  2. 关于更改背景壁纸(或执行特定任务):我认为我们可以创建一个启动器活动(透明的/不带 setContentView()),该活动将在触发更改壁纸的后台服务(或执行任何其他任务)后finish()本身。根据我的意见,虽然我个人没有尝试过,但这可以是上述场景的解决方案。

最好的问候,愉快编码 :)


2
简而言之: 由于您的应用程序图标已在随附的Android清单中注册,因此无法更改它。
根据文档 :
引用如下: 图标和标签 一些清单元素具有图标和标签属性,用于向用户显示相应应用程序组件的小图标和文本标签。
这意味着,由于清单文件无法在运行时更改,因此您的应用程序将始终具有相同的图标。 所以我猜测,您提到的应用程序是系统应用程序,并具有系统特权。
您唯一可以更改的图标是使用此权限创建的应用程序快捷方式:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

为了创建一个快捷方式,请查看这个答案:https://dev59.com/vlkS5IYBdhLWcg3wCShx#40446734
为了在单击应用程序图标时更改背景,请参考以下示例(在此示例中,我仅在应用程序打开时更改背景颜色): 类:
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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接