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

Android 添加和删除快捷方式

2011-12-31 18:52 225 查看
大家都应该知道的就是,我们能添加快捷方式,那么我们也能删除快捷方式,其实这个对我们新手来说就是一个在以后自己独立开发应用的时候的一个小例子,那么我们怎么样才能实现这两个方法那,我们用到最多的就是Intent这个,大家一定要记住,这个是必须有的,要是没有的话,我们就不会实现这个方法。好了不占用大家的时间了。直接上代码吧:

我们现在先来看看怎么样添加一个快捷方式:

/**
* 为程序创建桌面快捷方式
*/
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));
//快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
我们现在来看看删除的快捷方式:

private void delShortcut(){
Intent shortcut = new Intent(“com.android.launcher.action.UNINSTALL_SHORTCUT”);
//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
//指定当前的Activity为快捷方式启动的对象: 如 //com.everest.video.VideoPlayer
//注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
String appClass = this.getPackageName() + “.” +this.getLocalClassName();
ComponentName comp = new ComponentName(this.getPackageName(), appClass);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
sendBroadcast(shortcut);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: