有同学方反馈创建快捷方式后,点击快捷方式后不能启动程序或者提示"未安装程序",貌似是新的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. Java各个版本的新特性

    原链接:http://blog.csdn.net/shareus/article/details/50736159 1.5 1.自动装箱与拆箱: 2.枚举(常用来设计单例模式) http://www. ...

  2. hadoop hdfs 元数据 journalnode editslog fsimage

    先上图,文章以后再上 截图有先后 所以有些延迟,但是不耽误总体的理解(active-nn=a-nn=active-namenode; s-nn=standby-nn=standby-namenode; ...

  3. Flex学习笔记-时间触发器

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  4. usb之python(pyusb)

    电脑系统为WIN7 64位 python:为python3.6 32位 需要插件PyUSB-1.0.0.tar,pywinusb-0.4.2. 按照的步骤我偷懒了,自己百度一下. 我们先看设备管理的 ...

  5. flex 上下div固定, 中间div自适应

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. 爬虫--Scrapy-CrawlSpider&基于CrawlSpide的分布式爬虫

    CrawlSpider 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调par ...

  7. java配置slf4j日志系统

    首先要导入的包: import: 每个类中加入下面,其中 RdiFtpDownload.class 是当前的类名.class 然后就可以打日志了: 配置 log4j.properties log4j. ...

  8. javascript:控制一个元素高度始终等于浏览器高度

    window.onresize = function(){ this.opHtight()} //给浏览器添加窗口大小改变事件window.onresize = function(){ this.op ...

  9. SQL SERVER 2000安装遇到的问题小汇总(转载)

    [1]安装程序配置服务器失败需要修改下注册表1 打开注册表 在"开始"--"运行"键入 "regedit"  2 删除注册表如下键值: HK ...

  10. mysql 的sleep线程过多处理方法

    php程序不要使用长连接:java程序调整连接池 什么是长连接? 其实长连接是相对于通常的短连接而说的,也就是长时间保持客户端与服务端的连接状态. 通常的短连接操作步骤是: 连接->数据传输-& ...