关于快捷方式

来源:互联网 发布:mac vim python 配置 编辑:程序博客网 时间:2024/04/29 13:15

偶尔会用到快捷方式,贴一段代码,如有机型适配问题可以提出,一块解决。

    private void addShortcut() {    Intent shortcut = new Intent(SHORTCUT_INSTALL);        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortCut");        Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);        //不允许重复创建        shortcut.putExtra("duplicate",false); Intent intent = new Intent(this,Shortcuts.class);intent.setAction("com.ldj.shortcuts.self");    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);    this.sendBroadcast(shortcut);    }

删除快捷方式如下:

    private void deleteShortcut() {    Intent shortcut = new Intent(SHORTCUT_UNINSTALL);    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortCut");          //这里的intent要和创建时的intent设置一致    Intent intent = new Intent(this,this.getClass());    intent.setAction("com.ldj.shortcuts.self");       shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);      this.sendBroadcast(shortcut);    }      

需要添加权限:INSTALL_SHORTCUT和UNINSTALL_SHORTCUT

说明,这几篇帖子都以android 2.1系统测试为准

原创粉丝点击