Android应用创建桌面快捷图标

public static void addShortcut(Activity cx, String name) {
// TODO: 2017/6/25 创建快捷方式的intent广播
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// TODO: 2017/6/25 添加快捷名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 快捷图标是允许重复
shortcut.putExtra("duplicate", false);
// 快捷图标
Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(cx, R.mipmap.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
// TODO: 2017/6/25 我们下次启动要用的Intent信息
Intent carryIntent = new Intent(Intent.ACTION_MAIN);
carryIntent.putExtra("name", name);
carryIntent.setClassName(cx.getPackageName(),cx.getClass().getName());
carryIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//添加携带的Intent
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, carryIntent);
// TODO: 2017/6/25 发送广播
cx.sendBroadcast(shortcut);
}

http://www.360doc.com/content/16/0920/00/26211242_592141735.shtml
注意事项: 在设置 EXTRA_SHORTCUT_INTENT时,添加和删除快捷方式的这个INTENT对象的ACTION属性必须设置为相同内容,才能声明删除和创建的快捷方式是一个,进行绑定

Android桌面快捷方式那些事与那些坑# Android桌面快捷方式那些事与那些坑
https://www.cnblogs.com/waylife/p/android-shortcut-summary.html

public void addshotcut(Context context, String sName, Intent actionintent, Intent.ShortcutIconResource shortcutIconResource, boolean allow) {
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcut.putExtra("duplicate", allow);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, sName);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIconResource);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionintent);
    context.sendBroadcast(shortcut);
}

public void deleshortcut(Context context, String sName, Intent actionintent, boolean allow) {
    Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, sName);
    shortcut.putExtra("duplicate", allow);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionintent);
    context.sendBroadcast(shortcut);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,569评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 北京的香水梨(又叫南果梨),好吃,说说味道,皮软软酥酥,用力一洗便破了,然后是丰满的汁水流出,甜甜蜜蜜,关键是一口...
    秒针滴滴答阅读 405评论 0 1
  • 卖辣椒的人总会遇到这样的问题,“你这辣椒辣吗?”怎么回答呢?说辣吧,怕辣的人,立马走了;答不辣吧,也许人家喜欢吃辣...
    紸禵曲阅读 282评论 0 1
  • 遇见的你是一个午后 带远方的气息 叶子归来 也像是遇见了你 你是诗人 一颗菩提的种子,麦芒 赠与一片黄土 土地中诗...
    红尘红尘阅读 244评论 0 1