在做app过程中,有可能在设置里面有设置添加桌面快加方式的功能。下面就添加shortcut 研究下
首先判断一下桌面是否已经添加了快捷方式:
判断api版本 以2.2版本的api 为判断标准
通过ContentProvier查询快捷方式数据
String url="";
if(android.os.Build.VERSION.SDK_INT<8){
url="content://com.android.launcer.settings/favorites?notify=true";
}else{
url="content://com.android.launcer.settings2/favorites?notify=true";
}
ContentResolver resolver=context.getContentResolver(); //内容解析器
Cursor cursor=resolver.query(Uri.parse(url),null,"title=?",new String[]{context.getString(R.string.app_name)},null);
if(cursor!=null&&cursor.moveToFirst()){
cursor.close();
}
判定完毕符合条件的话执行addshortcut方法
String ACTION_ADD_SHORTCUT="com.android.launcer.action.INSTALL_SHORTCUT"; //Intent 添加快捷方式 的action值
//也可以调用Manifest 类里面的静态低层内部类permission里的常量 INSTALL_SHORTCUT 但是api大于等于9
Intent intent=new Intent(ACTION_ADD_SHORTCUT);
ShortcutIconResource icon=Intent.ShortcutIconResource.fromContext(context,R.drawable.icon); //获取快件方式图片
intent.putExtra("duplicate",false);//不允许重复创建
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON.RESOURCE,icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));
//设置切入的主activity
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(getApplicationContext(),AppMainActivity.class));
//或者使用component
Intent maincutintent=new Intent();
maincutintent.setComponent(new ComponentName(context.getPackageName(),context.getPackageName()+".AppMainActivity"));
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,maincutintent);
context.sendBoradcast(intent); //发送广播
删除快件方式使用
action 只为 com.android.launcher.action.UNINSTALL_SHORTCUT 的intent执行任务
原文:http://blog.csdn.net/duty_is_codeing/article/details/23662841