注释已经说得很清楚了,如果有疑问,请留言

/**
* 添加桌面快捷方式
*
* @param view
*/
public void click1(View view) {
if (isExit()) {
Toast.makeText(getApplicationContext(), "快捷方式已经存在", 0).show();
return;
}
// 取得图标资源
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.logo);
Intent intent = new Intent();
// 设置意图的动作
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// 设置图标的名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "LOL");
// 设置图标
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 设置意图的动作和类别
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_MAIN);
intent2.addCategory(Intent.CATEGORY_LAUNCHER);
intent2.setComponent(new ComponentName(this, MainActivity.class));
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
sendBroadcast(intent);
}
/**
* 删除桌面快捷方式
*
* @param view
*/
public void click2(View view) {
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.logo);
Intent intent = new Intent();
// 设置意图的动作
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
// 设置图标的名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "LOL");
// 设置图标
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 设置意图的动作和类别
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_MAIN);
intent2.addCategory(Intent.CATEGORY_LAUNCHER);
intent2.setComponent(new ComponentName(this, MainActivity.class));
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
sendBroadcast(intent);
}
/**
* 判断桌面是否已经存在快捷方式
*
* @return
*/
private boolean isExit() {
Uri uri = null;
// 不同版本中的Uri不同
if (getSdkVersion() < 8) {
uri = Uri
.parse("content://com.android.launcher.settings/favorites");
} else {
uri = Uri
.parse("content://com.android.launcher2.settings/favorites");
}
String selection = "title=?";
String[] selectionArgs = new String[] { "LOL" };
Cursor cursor = getContentResolver().query(uri, null, selection,
selectionArgs, null);
if (cursor.moveToNext()) {
cursor.close();
return true;
} else {
cursor.close();
return false;
}
}
/**
* 获得手机SDK的版本
*
* @return
*/
private int getSdkVersion() {
return android.os.Build.VERSION.SDK_INT;
}

需要声明的权限


    <!-- 添加快捷方式权限 -->

    <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"/>


点击下载源代码

【Android进阶】快捷图标的创建与移除的更多相关文章

  1. 【Android进阶】为什么要创建Activity基类以及Activity基类中一般有哪些方法

    现在也算是刚刚基本完成了自己的第一个商业项目,在开发的过程中,参考了不少人的代码风格,然而随着工作经验的积累,终于开始慢慢的了解到抽象思想在面向对象编程中的重要性,这一篇简单的介绍一下我的一点收获. ...

  2. CentOS Android Studio桌面图标的创建

    1.切换到root用户,在桌面上创建Android.Studio.desktop,如下: [Desktop Entry] Name=Android Studio Comment=Android Stu ...

  3. 安卓开发之APK安装之后自动在桌面上创建快捷图标

    可以看到很多的APP在第一次运行之后就会弹出来一个Toast说什么快捷方式已创建,那么这个东西是怎么搞出来的呢 很简单就下面几句话,写在这儿以后好copy 先创建一个类 import android. ...

  4. Android进阶(二十八)上下文菜单ContextMenu使用案例

    上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...

  5. 我的Android进阶之旅------>经典的大牛博客推荐(排名不分先后)!!

    本文来自:http://blog.csdn.net/ouyang_peng/article/details/11358405 今天看到一篇文章,收藏了很多大牛的博客,在这里分享一下 谦虚的天下 柳志超 ...

  6. Android开发面试经——4.常见Android进阶笔试题(更新中...)

      Android开发(29)  版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http:/ ...

  7. 我的Android进阶之旅------&gt; Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包括图像的文本信息).并简要说明实现方法. 答案:Android SDK支持例如以下显示富文本信息的方式. 1.使用T ...

  8. Android进阶 | 摆脱斗图 | 行业交流 | 深度学习 | 付费

    Android进阶 | 摆脱斗图 | 行业交流 | 深度学习 | 付费 其实在很早的时候我就有想过,是不是退出一些群,因为群太多了,里面的水友也多,基友也多,就难免会水起来,这样既耽误学习又耽误工作, ...

  9. Android进阶(二十)AndroidAPP开发问题汇总(四)

    · Android进阶(二十)AndroidAPP开发问题汇总(四) android:layout_width和android:width的区别 基中的android:layout_width和and ...

随机推荐

  1. 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)

    上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...

  2. Berkeley DB 使用

    http://www.ibm.com/developerworks/cn/linux/l-embdb/index.html UNIX/LINUX平台下的数据库种类非常多,参考资料1中 列举了其中的大部 ...

  3. 灰度图像阈值化分割常见方法总结及VC实现

    转载地址:http://blog.csdn.net/likezhaobin/article/details/6915755 在图像处理领域,二值图像运算量小,并且能够体现图像的关键特征,因此被广泛使用 ...

  4. 安装dotnet core

    CentOS 7.1下安装dotnet core .NET CORE的官方(http://dotnet.github.io/getting-started/)只提供了Windows, Ubuntu14 ...

  5. 可兼容IE的jquery.cookie函数方法

    前言 在开发过程中,因为之前有接触过Discuz,就直接拿其common.js里面的getcookie和setcookie方法来使用,做到后面在使用IE来测试的时候,发现这两个方法子啊IE下不起作用, ...

  6. Scroll View 控件以Thumbnail的方式显示一个目录的全部图片,相似图片浏览器

    MAC : XCode -> Scroll View 控件以Thumbnail的方式显示一个目录的全部图片,类似图片浏览器 STEP1:将两个目录复制到project里面ImageBrowser ...

  7. poj3694(动态询问割桥的数目)

    给我们一个图,然后有q次加边的操作,问每次加完边之后有多少个桥存在 首先用dfs求出所有的桥,然后dfs的过程中生成了一棵dfs树,该树有的边是桥,有的不是,用bridge[v] = true , 表 ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图:   从上图我们可以 ...

  9. vb实现多用户登录

    利用vb实现多用户登录,主要是将vb与数据库实现链接,这个问题在作品展中我们的软件“天天迹录”的登录时用到,但是当时自己只是知道有这么个功能,但是那些代码的含义并不明白,在3个月后的我又有机会接触到它 ...

  10. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(一)

    ArcGIS 10.2 for Desktop是在10.1的成功基础上进行的改进,它的改进包括:性能提升.附加的安全性.40多个新的分析工具.3D功能提高.栅格增强.新的地理数据管理能力以及其它更多的 ...