gpt4 book ai didi

android - 创建应用程序图标

转载 作者:行者123 更新时间:2023-11-29 22:07:56 26 4
gpt4 key购买 nike

如何为主 Activity 创建应用程序图标?

我想创建一个快捷方式来在主屏幕页面上自定义我的应用程序图标。

我希望在安装应用程序后将其应用到主屏幕上。

我该怎么做?

最佳答案

添加快捷方式的方法

private void addShortcut(){
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

// Shortcut name
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
shortcutIntent.putExtra("duplicate", false);

ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

// Shortcut icon
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

sendBroadcast(shortcutIntent);
}

删除快捷方式的方法

private void delShortcut(){
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

// Shortcut adı
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

String appClass = this.getPackageName() + "." +this.getLocalClassName();
ComponentName comp = new ComponentName(this.getPackageName(), appClass);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

sendBroadcast(shortcut);
}

在 OnCreate 上调用方法

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
delShortcut();
addShortcut();

添加快捷方式的权限

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

关于android - 创建应用程序图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10346723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com