测试环境为Adnroid 2.1以上。

第一步:AndroidManifest.xml 权限配置:

添加快捷方式权限:

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

验证快捷方式是否存在权限:
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

删除快捷方式权限:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

代码:

 1 public class ShortCutSample { 2     /** 3     * 添加快捷方式 4     * */ 5     public void creatShortCut(Activity activity,String shortcutName,int resourceId) 6     { 7         Intent intent = new Intent();  8         intent.setClass(activity, activity.getClass());   9         /*以下两句是为了在卸载应用的时候同时删除桌面快捷方式*/10         intent.setAction("android.intent.action.MAIN");  11         intent.addCategory("android.intent.category.LAUNCHER");  12         13         Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");14          15          //不允许重复创建16          shortcutintent.putExtra("duplicate", false);17          //需要现实的名称18          shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);19          //快捷图片20          Parcelable icon = Intent.ShortcutIconResource.fromContext(activity.getApplicationContext(), resourceId);21          shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);22          //点击快捷图片,运行的程序主入口23          shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);24          //发送广播。OK25          activity.sendBroadcast(shortcutintent);26     }27     /**28     * 删除快捷方式29     * */30     public void deleteShortCut(Activity activity,String shortcutName)31     {32         Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");  33         //快捷方式的名称  34         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName);  35         //在网上看到到的基本都是一下几句,测试的时候发现并不能删除快捷方式。36         //String appClass = activity.getPackageName()+"."+ activity.getLocalClassName();  37         //ComponentName comp = new ComponentName( activity.getPackageName(), appClass);  38         //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));   39         /**改成以下方式能够成功删除,估计是删除和创建需要对应才能找到快捷方式并成功删除**/40         Intent intent = new Intent(); 41         intent.setClass(activity, activity.getClass());  42         intent.setAction("android.intent.action.MAIN");  43         intent.addCategory("android.intent.category.LAUNCHER");  44         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);  45         activity.sendBroadcast(shortcut);          46     }47     /**48     * 判断是否存在快捷方式49     * */50     public boolean hasShortcut(Activity activity,String shortcutName)51     {52         String url = ""; 53         int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK);54         /*大于8的时候在com.android.launcher2.settings 里查询(未测试)*/55         if(systemversion < 8){ 56               url = "content://com.android.launcher.settings/favorites?notify=true"; 57         }else{ 58             url = "content://com.android.launcher2.settings/favorites?notify=true"; 59         } 60         ContentResolver resolver = activity.getContentResolver(); 61         Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",new String[] {shortcutName}, null); 62         if (cursor != null && cursor.moveToFirst()) { 63                 cursor.close(); 64                 return true; 65         } 66         return false; 67     }68 }

调用测试代码:

   public class mainActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ShortCutSample sample =new ShortCutSample();        String shortcutName=getString(R.string.app_name);                if(sample.hasShortcut(this, shortcutName))        sample.deleteShortCut(this,shortcutName);        else        sample.creatShortCut(this,shortcutName,R.drawable.icon);            }}

在网上找了很久都是一样的代码,删除那块搞了一个下午才弄好,其实很简单的东东。

第一次发文章,Adnroid新人。多多交流和指导呀。呵呵。

更多相关文章

  1. Android单元测试总结
  2. android代码分析,及Terminal使用
  3. Android应用程序绑定服务(bindService)的过程源代码分析(2)
  4. android 源码下载、编译和测试
  5. Android android 6.0权限校验及版本兼容问题
  6. android 单元测试之dialog测试链接整理
  7. [导入]Android代码优化
  8. android APP隐私政策弹框的实现代码实例
  9. Android 读取一个已经安装的包的权限

随机推荐

  1. android热修复AndFix使用记录
  2. Android触摸屏坐标转换
  3. Android(安卓)Studio 使用本地Maven仓库
  4. Android检测服务是否运行
  5. Android源代码在线浏览网站列表
  6. Android 性能优化概念(1)
  7. Android WebView自定义处理错误页面显示(
  8. android I/O
  9. Android 获取 联系人信息
  10. Android(安卓)无法通过cmcc wap2.0 test