思路:

  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. objective C中数据持久化方式1--对象归档

    第一.数据持久化的方式: NSKeyedArchiver--对象归档 属性列表化(NSArray.NSDictionary.NSUserDefault) SQlite数据库.CoreData数据库 其 ...

  2. Vim插件列表

    01.helm(Vim-Swoop) 02.ap/vim-buftabline 03.wesleyche/SrcExpl 04.vim proc 05.vim shell 06.dhruvasagar ...

  3. 在Ubuntu 12.04安装和设置Samba实现网上邻居共享

    转载:http://www.startos.com/ubuntu/tips/2012031333097.html          有微小改动. Samba 是一款功能强大的共享工具,可以实现与win ...

  4. 在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources

    Download the code for this article: WinForm-Multilanguages-2.rar (11 KB). 方法二: 下面介绍一种只需对现有代码做较小改动的方法 ...

  5. tcp连接的3次握手

    http://www.tcpipguide.com/free/t_TCPConnectionEstablishmentProcessTheThreeWayHandsh-3.htm synchronou ...

  6. NPOI的测试代码

    NPOI\testcases\main\testcases vs10.csproj 需要注意,重新引用一下NPOI类库 需要注意的是,测试项目,使用了NUnit 找到测试项目下的SS文件夹,再定位到U ...

  7. shorter concat [reverse longer]

    shorter concat [reverse longer] Description: Given 2 strings, a and b, return a string of the form:  ...

  8. ORACLE-修改当前会话的语言环境

    修改当前会话的语言环境变量参数:ALTER SESSION SET =将语言改为英语: SQL> ALTER SESSION SET NLS_LANGUAGE='AMERICAN';SESSIO ...

  9. C语言计算程序运行时间

    #include<stdio.h>#include<stdlib.h> #include "time.h" int main( void )  {     ...

  10. Android开发之错误:elicpse运行时弹出Running Android Lint has encountered a problem failed, nullpointerexception

    昨天安装了下Android Studio,把SDK路径指向了ADT目录下的SDK目录.同时FQ出去更新了下SDK.然后今天运行eclipse的时候,弹出错误,同时在工程的名称处有错误提醒,但是代码中没 ...