Android 向桌面添加快捷方式

samool 发布暂无评论8383 阅读

AI摘要

文章介绍了如何在Android系统中向桌面添加快捷方式。通过编写代码,使用Intent对象设置快捷方式的名称、启动的Activity和图标资源,然后发送广播以创建快捷方式。同时,需要在AndroidManifest.xml文件中添加相应的权限。

向桌面添加一个快捷方式

privatevoidaddShortcut(){
Intent shortcut
=newIntent("com.android.launcher.action.INSTALL_SHORTCUT");

//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
//shortcut.putExtra("duplicate", false);//不允许重复创建

//指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
//注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
//ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());

//shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(this,ShortcutTest.class));

//快捷方式的图标
ShortcutIconResource iconRes=Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

sendBroadcast(shortcut);
}

   shortcut.putExtra("duplicate", false); //表示的为不允许重复设置

此种代码,直接添加到桌面上,并且也在上图所示的地方显示,但是此需要添加下面的权限: 

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

  1. 01CentOS7 + 宝塔环境搭建 Gogs 私人Git 仓库2020-09-01
  2. 02android将程序快捷方式添加到“添加快捷方式窗口”2011-07-09
  3. 03TurboLaunch v5.0.10 汉化版2007-09-07
  4. 04SaMDeditor V2.4 更新内容2026-12-02
  5. 05恭喜《轻武爱学习》小程序备案成功!2026-11-24
  6. 06SaView 图阅 1.1.1 更新日志2026-11-18
  7. 07Book2EPUB 1.0.4 更新日志2026-11-18
  8. 08SaView 图阅 一款轻便纯粹的看图工具2026-11-16
  9. 09免费工具Book2EPUB:轻松将电子书转换为 EPUB2026-11-11
  10. 10SaBlog2026 主题功能介绍(v1.1.36)2026-10-26
COMMENTS / 一纸留言

读者留言 0 条

一纸留言,便是山水间的一次相逢。

还没有留言,欢迎写下第一段交流。

WRITE A COMMENT

写下你的留言