Android 桌面快捷方式的设计
有时候为了使用方便,需要在桌面上添加快捷方式,下面是两种添加快捷方式的方法:
1.
代码
void setshortCut() { Intent addShortcut = new Intent(); // 设置快捷方式的名字
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式练习");
// 构建快捷方式中专门的图标
Parcelable icon = Intent.ShortcutIconResource.fromContext( ShortcutTest.this, R.drawable.icon);
// 添加快捷方式图标
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 构建快捷方式执行的Intent Intent mailto = new Intent(this, ShortcutTest.class); // 添加快捷方式
Intent addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mailto); // 正常 setResult(RESULT_OK, addShortcut);
}
通过以上代码添加的快捷方式,只能在程序启动中添加,不能加入到事件中,现在也没有想明白
是什么,也没有仔细研究一下,希望知道的朋友通知一下,并且这样添加的时候,不直接添加到
桌面中,而是添加到添加快捷方式窗口中,如图:
2.
代码
private void addShortcut(){ Intent shortcut = new
Intent("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, new Intent(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);
//表示的为不允许重复设置
此种代码,直接添加到桌面上,并且也在上图所示的地方显示,但是此需要添加下面的权限:
Android桌面快捷方式图标生成与删除,使用Intent与launcher交互
通过分析Launcher的生成快捷方式的过程,找出了使用Intent发送请求,Launcher
通过自己注册的InstallShortCutReceiver和UnInstallShortCutReceiver实现了快捷方
式图标的生成与移除过程。本文主要分析外部apk如何使用Intent请求生成快捷
方式和移除快捷方式图标的问题。
生成快捷方式代码:
Java代码
1. private static final String ACTION_INSTALL_SHORTCUT =
2. "com.android.launcher.action.INSTALL_SHORTCUT";
3.
4. /**
5. * 是否可以有多个快捷方式的副本
6. */
7. static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
8.
9. Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
10. shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
11. getString(R.string.app_name));
12. // 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷
方式,如果是false就不会重复添加
13. shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
14. Intent intent2 = new Intent(Intent.ACTION_MAIN);
15. intent2.addCategory(Intent.CATEGORY_LAUNCHER);
16.
17. // 要删除的应用程序的ComponentName,即应用程序包名+activity的名字
18. intent2.setComponent(new ComponentName(this.getPackageName(),
19. this.getPackageName()+".Main"));
20.
21. shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
22. shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
23. Intent.ShortcutIconResource.fromContext(this,
24. R.drawable.icon));
25. sendBroadcast(shortcutIntent);
注:Intent intent2 = new Intent(Intent.ACTION_MAIN); 这个也可以换成的构造参数
也可以是Intent.ACTION_CREATE_SHORTCUT,也可以生成快捷方式图标,但是
这样不标准,在删除的时候如果不和这个对于相同则无法删除。所以还是用
Intent.ACTION_MAIN。
那么删除快捷方式的代码是:
Java代码
1. private static final String ACTION_UNINSTALL_SHORTCUT =
2. "com.android.launcher.action.UNINSTALL_SHORTCUT";
3.
4.
5.
6. Intent intent = new Intent(ACTION_UNINSTALL_SHORTCUT );
7. intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
8. // 要删除的应用程序的ComponentName,即应用程序包名+activity的名字
9. ComponentName comp = new ComponentName(info.activityInfo.packageName,
10. info.activityInfo.name);
11. intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()
12. .setComponent(comp).setAction("android.intent.action.MAIN"));
13. sendBroadcast(intent);
14.
15.
16. 最后要记得加上权限:
17. 添加的权限:
18. 删除的权限:
本文档为【Android 桌面快捷方式的设计】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。