思路:

  Launcher为了应用程序能够定制自己的快捷图标,就注册了一个 BroadcastReceiver 专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该 BroadcastReceiver 构造出相对应的Intent并装入我们的定制信息,最后调用 sendBroadcast 方法就可以创建一个快捷图标了。

步骤:

  1. 创建快捷方式必须要有权限;
  2. 创建快捷方式的广播的 Intent 的 action 设置 com.android.launcher.action.INSTALL_SHORTCUT
  3. 删除快捷方式的广播的 Intent 的 action 设置 com.android.launcher.action.UNINSTALL_SHORTCUT
  4. 设置快捷方式的图片和名称等信息放在 Intent 中;

  需要添加的权限如下:

   <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS"/>

  核心代码为:

   /**
* 添加当前应用的桌面快捷方式
*
* @param context
*/
public static void addShortcut(Context context, int appIcon) {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT"); Intent shortcutIntent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 获取当前应用名称
String title = null;
try {
final PackageManager pm = context.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
// 不允许重复创建(不一定有效)
shortcut.putExtra("duplicate", false);
// 快捷方式的图标
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(context,
appIcon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); context.sendBroadcast(shortcut);
} /**
* 删除当前应用的桌面快捷方式
*
* @param context
*/
public static void delShortcut(Context context) {
Intent shortcut = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT"); // 获取当前应用名称
String title = null;
try {
final PackageManager pm = context.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Intent shortcutIntent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
context.sendBroadcast(shortcut);
} /**
* 判断当前应用在桌面是否有桌面快捷方式
*
* @param context
*/
public static boolean hasShortcut(Context context) {
boolean result = false;
String title = null;
try {
final PackageManager pm = context.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) { } final String uriStr;
if (android.os.Build.VERSION.SDK_INT < 8) {
uriStr = "content://com.android.launcher.settings/favorites?notify=true";
} else if (android.os.Build.VERSION.SDK_INT < 19) {
uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
} else {
uriStr = "content://com.android.launcher3.settings/favorites?notify=true";
}
final Uri CONTENT_URI = Uri.parse(uriStr);
final Cursor c = context.getContentResolver().query(CONTENT_URI, null,
"title=?", new String[]{title}, null);
if (c != null && c.getCount() > 0) {
result = true;
}
return result;
}

Android 添加、移除和判断 桌面快捷方式图标的更多相关文章

  1. C#项目打包后安装的桌面快捷方式图标怎么设置成自己想要的图标

    #项目打包后安装的桌面快捷方式图标怎么设置成自己想要的图标 2012-08-25 09:11匿名 | 浏览 3286 次  C#编程 C#项目用vs2005自带的工具打包后安装的桌面快捷方式图标怎么设 ...

  2. Windows桌面快捷方式图标全部变成同一个图标的解决方法

    今天来个客人,说是电脑的所有程序打开都变成 Adobe Reader 了,打开看了下,刚开始是以为EXE文件关联被修改了,用注册表修复工具弄了下,重启电脑,还是老样子.仔细看了下,原来只是快捷方式变成 ...

  3. Android桌面快捷方式那些事与那些坑

    原文来自http://blog.zanlabs.com/2015/03/14/android-shortcut-summary/ 将近二个多月没写博客了.之前一段时间一直在搞红包助手,就没抽时间写博客 ...

  4. 如何实现桌面App图标可以动态显示消息数(类似手机上的QQ图标)?

    手机上的APP , 像QQ和微信等都可以在图标上动态显示消息数(最大99) , 那么你有没有想过这些效果是如何实现的?桌面上开发的传统应用程序能否也实现类似的功能? 1 思路 桌面快捷方式的图标本质上 ...

  5. Android添加快捷方式(Shortcut)到手机桌面

    Android添加快捷方式(Short)到手机桌面 权限 要在手机桌面上添加快捷方式,首先需要在manifest中添加权限. <!-- 添加快捷方式 --> <uses-permis ...

  6. Android开发被添加到桌面快捷方式

    Android开发被添加到桌面快捷方式 对于一个希望拥有很多其它用户的应用来说.用户桌面能够说是全部软件的必争之地,假设用户在手机桌面上建立了该软件的快捷方式.用户将会更频繁地使用该软件. 因此,全部 ...

  7. Android 添加桌面快捷方式操作

    /** * 为程序创建桌面快捷方式 */ private void addShortcut(){ Intent shortcut = new Intent(“com.android.launcher. ...

  8. (转)Android创建桌面快捷方式两种方法

    [IT168技术]Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成. 谈谈在桌面上直接生成.个人觉得这个比较爽快,既然都是快捷方式了干嘛还 ...

  9. Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity

    我们一般下载的应用在第一次启动应用的时候都会给我创建一个桌面快捷方式,然后我在网上找了些资料整理下了,写了一个快捷方式的工具类,这样我们以后要创建快捷方式的时候直接拷贝这个类,里面提供了一些静态方法, ...

随机推荐

  1. Java Socket实战之一 单线程通信

    本文地址:http://blog.csdn.net/kongxx/article/details/7259436 现在做Java直接使用Socket的情况是越来越少,因为有很多的选择可选,比如说可以用 ...

  2. Android:activity跳转过渡效果

    放在startActivity(intent);后面 overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out) ...

  3. Oracle 学习笔记 常用查询命令篇

    1.查询某个用户下有多少张表 有时候很有用  select count(*) from dba_tables t where t.owner='SCOTT';

  4. 1837. Isenbaev's Number(floyd)

    1837 被数据结构部分打击的不行了 换地 刷点简单的 图论第一题 floyd水过 #include <iostream> #include<cstdio> #include& ...

  5. 在SQLite中使用索引优化查询速度

    在进行多个表联合查询的时候,使用索引可以显著的提高速度,刚才用SQLite做了一下测试. 建立三个表: create table t1 (id integer primary key,num inte ...

  6. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

  7. Andriod x86 4,4

    用 vbox 装了下Andriod x86 4,4 ,能用的App真心少,反正我只是用来看看安卓浏览器里网站效果,海豚浏览器还有qq浏览器x86版,还能用用,主要要换UA为iphone和ipad之类的 ...

  8. poj2406 周期

    脑残wa了一次 var s:ansistring; ans,i,k,m:longint; pre:..] of longint; function max(x,y:longint):longint; ...

  9. RPi 2B GPIO 测试

    /************************************************************************************** * RPi 2B GPI ...

  10. acdream 瑶瑶带你玩激光坦克 (模拟)

    瑶瑶带你玩激光坦克 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 256000/128000KB (Java/Others) Submi ...