您的位置:首页 > 移动开发 > Android开发

Android代码添加快捷方式与删除快捷方式

2013-01-07 14:54 351 查看
添加桌面快捷方式代码如下:

/*
*新加添加快捷方式代码
*/
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);
//设置快捷方式图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
//设置快捷方式启动入口
ComponentName comp = new ComponentName(this.getPackageName(), LanuchActivity.class.getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
//发送广播,添加快捷方式
sendBroadcast(shortcut);


其中,shortcut.putExtra("duplicate", false);设置是否可以重复添加在某些厂商的定制launcher上不起作用,可以重复添加,比如华为桌面。而在360桌面上就可以起作用。这点需要注意。

卸载桌面快捷方式代码如下:

//设置快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
//设置快捷方式启动入口
ComponentName comp = new ComponentName(getPackageName(), LanuchActivity.class.getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

//发送卸载快捷方式的图标
sendBroadcast(shortcut);


这段代码在某些厂商的定制launcher上不起作用,无法卸载快捷方式,比如华为桌面。360桌面上测试可以使用。

需要在manifest文件中声明使用权限:

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

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: