有同学方反馈创建快捷方式后,点击快捷方式后不能启动程序或者提示"未安装程序",貌似是新的rom在快捷方式这块做过修改(由于此文是11年5月所出,估计应该是2.0或2.1的rom),现已修正,HTC G11 2.3.5rom测试通过.

1,判断是否已经创建了快捷方式(在某些机型中需要判断)

1
2
3
4
5
6
7
8
9
10
11
12
13
private boolean hasShortcut()
{
        boolean isInstallShortcut = false;
        final ContentResolver cr = activity.getContentResolver();
        final String AUTHORITY ="com.android.launcher.settings";
        final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true");
        Cursor c = cr.query(CONTENT_URI,new String[] {"title","iconResource" },"title=?",
        new String[] {mapViewActivity.getString(R.string.app_name).trim()}, null);
        if(c!=null && c.getCount()>0){
            isInstallShortcut = true ;
        }
        return isInstallShortcut ;
    }

2, 创建

1
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
     * 为程序创建桌面快捷方式
     */ 
    private void addShortcut(){ 
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
               
        //快捷方式的名称 
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
        shortcut.putExtra("duplicate"false); //不允许重复创建 
 
        /****************************此方法已失效*************************/
        //ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName()); 
        //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));    
     /******************************end*******************************/
     Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
     shortcutIntent.setClassName(thisthis.getClass().getName());
     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
 
        //快捷方式的图标 
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); 
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); 
               
        sendBroadcast(shortcut); 
    

  

3, 删除

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<strong>    /**
     * 删除程序的快捷方式
     */ 
    private void delShortcut(){ 
        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
               
        //快捷方式的名称 
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
        String appClass = this.getPackageName() + "." +this.getLocalClassName(); 
        ComponentName comp = new ComponentName(this.getPackageName(), appClass); 
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); 
               
        sendBroadcast(shortcut); 
               
    
</strong>

3, 声明权限

在AndroidManifest.xml 文件中声明 创建和删除快捷方式时声明权限

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

Android创建和删除桌面快捷方式的更多相关文章

  1. Python创建、删除桌面、启动组快捷方式的例子分享

    一.Python创桌面建快捷方式的2个例子 例子一: 代码如下: import osimport pythoncomfrom win32com.shell import shell    from w ...

  2. ubuntu创建文件夹桌面快捷方式

    最近在使用dropbox,用来存储一些自己的markdown笔记和pdf文件.觉得放一个快捷方式在桌面上比较方便,但是lxde似乎没有直接创建桌面快捷方式的功能(或者是我没有找到),就上网查了一下,顺 ...

  3. Android 通过程序添加桌面快捷方式

    原理:通过代码向 Launcher 中的广播接收者发送广播来创建快捷图标. 首先要声明的权限是: <!--添加图标的权限--> <uses-permission android:na ...

  4. Android创建或删除了文件,在电脑端查看的时候,却没有对应的变化,处理办法

    在Android应用中,碰到一个问题,在代码中执行创建或者删除某个文件后,在系统的文件管理器中能够相应地看到文件的变化,但是插在电脑上查看的时候,却看不到改文件的变化.同时,当创建文件后,在系统中的某 ...

  5. Win10创建mysql8.0桌面快捷方式以及启动mysql.exe闪退问题

    1.先找到mysql的bin目录,将Mysql.exe发送快捷方式到桌面. 2.然后右键选择属性,将目标后面添加上 -uroot -p 我的完整目标如下: E:\mysql-8.0.17-winx64 ...

  6. Android -- 创建桌面快捷方式

    代码                                                                                    /** * * 返回添加到桌 ...

  7. Ubuntu创建桌面快捷方式

    默认情况下,ubuntu会将自动安装的软件快捷方式保存在/usr/share/applications目录下,如果我们要创建桌面快捷方式,只需要右键-复制-桌面 就Ok,如图: 上面的方法是通过系统自 ...

  8. ubuntu系统下创建软件桌面快捷方式

    转自ubuntu系统下创建软件桌面快捷方式 默认情况下,ubuntu会将自动安装的软件快捷方式保存在/usr/share/applications目录下,如果我们要创建桌面快捷方式,只需要右键-复制- ...

  9. c#: 创建桌面快捷方式

    App适配输出方式时发现问题,聊做备忘. 需要注意的是:不要直接引用Interop.IWshRuntimeLibrary.dll程序集,因为它可能是x86或x64的,倘若程序以Any CPU方式编译, ...

随机推荐

  1. django之ReverseOneToOneDescriptor

    class ReverseOneToOneDescriptor(object): """ Accessor to the related object on the re ...

  2. linux驱动开发第一步hello

    先查看Ubuntu的版本 cat /etc/issue lin@lin-virtual-machine:~$ cat /etc/issue Ubuntu 12.04 LTS \n \l 或者使用 li ...

  3. jquery源码'jQuery.fn.init.prototype'

    一般我们在创建构造函数即使用的时候会这样写,使用的时候会使用new 关键字,先实例化,然后使用. function test(name, age) { this.name = name; this.a ...

  4. js的非空校验

    利用TagName获取元素名称,进行批量非空校验 var input = document.getElementsByTagName("input"); for (var i=0; ...

  5. 池以及barrier简单

    用了下CyclicBarrier,注意线程池中的线程数量设置,还有就是DB连接的时候,需要考虑单个DB能承受的最大连接数目和每个连接上能同时打开的cursor等限制,需要时可以通过jstack查看堆栈 ...

  6. eclipse中导入java类失败的问题

    在 Eclips 开发时,新建了一个 Dynamic Web Project,在运行jsp文件时tomcat报错: org.apache.jasper.JasperException: Unable ...

  7. python语言中的数据类型之集合

    数据类型 集合类型    set 用途:1.关系运算        2.去重 定义方式:在{}内用逗号分隔开多个元素,但元素的特点是 1.集合内元素必须是不可变类型 2.集合内元素无序 集合内元素不能 ...

  8. linux 3.10 一次softlock排查

    x86架构.一个同事分析的crash,我在他基础上再次协助分析,也没有获得进展,只是记录一下分析过程.记录是指备忘,万一有人解决过,也好给我们点帮助. 有一次软锁,大多数cpu被锁,log中第一个认为 ...

  9. ReactiveX 学习笔记(7)聚合操作符

    Mathematical and Aggregate Operators 本文的主题为处理 Observable 的聚合操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操 ...

  10. delphi 大文件的读写 使用 MapviewOffile

    unit filemap; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...